如何在C#中将字符串日期转换为以下日期格式dd / MM / yyyy t:m:s(How to convert string date to following date format dd/MM/

编程入门 行业动态 更新时间:2024-10-25 07:21:48
如何在C#中将字符串日期转换为以下日期格式dd / MM / yyyy t:m:s(How to convert string date to following date format dd/MM/yyyy t:m:s in C#)

我需要将日期格式从字符串转换为dd/MM/yyyy tt:mm:ss in C#例如convert

string = "2015-07-21T23:00:00.000Z"

{21/07/2015 00:00:00}

I need to convert date format from string to dd/MM/yyyy tt:mm:ss in C# for example convert

string = "2015-07-21T23:00:00.000Z"

to

{21/07/2015 00:00:00}

最满意答案

我会使用DateTimeStyles.RoundtripKind枚举将其解析为DateTime ,因为它是ISO 8601格式,然后使用它的Date属性将它的时间部分设置为午夜。

var dt = DateTime.Parse("2015-07-21T23:00:00.000Z", null, DateTimeStyles.RoundtripKind); Console.WriteLine(dt.Date.ToString("dd'/'MM'/'yyyy HH:mm:ss")); // 21/07/2015 00:00:00

这是一个demonstration

I would parse it to DateTime with DateTimeStyles.RoundtripKind enumeration since it is ISO 8601 format then use it's Date property to set it's time part to midnight.

var dt = DateTime.Parse("2015-07-21T23:00:00.000Z", null, DateTimeStyles.RoundtripKind); Console.WriteLine(dt.Date.ToString("dd'/'MM'/'yyyy HH:mm:ss")); // 21/07/2015 00:00:00

Here a demonstration.

更多推荐

本文发布于:2023-07-18 04:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   字符串   转换为   中将   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!