如何在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:00Here a demonstration.
更多推荐
发布评论