简体   繁体   English

如何在C#中将字符串日期转换为以下日期格式dd / MM / yyyy t:m:s

[英]How to convert string date to following date format dd/MM/yyyy t:m:s in C#

I need to convert date format from string to dd/MM/yyyy tt:mm:ss in C# for example convert 我需要将日期格式从字符串转换为C#中的dd/MM/yyyy tt:mm:ss ,例如convert

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

to

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

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. 我将使用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

Here a demonstration . 这里有demonstration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM