简体   繁体   English

如何再次将以下字符串转换为日期字符串?

[英]How to convert the following string to date string again?

I have a string passed from kendo grid filter as follows, 我从kendo网格过滤器传递了一个字符串,如下所示:

2018-05-01T18:30:00.000Z

i need to conver the above to this format, 我需要将以上内容转换为这种格式,

May 05 2018

i tried with the following, 我尝试了以下

string  date = (DateTime.ParseExact(constant.ToString(), "MMM dd yyyy", CultureInfo.InvariantCulture)).ToString("MMM dd yyyy");

bit its not working as expected 有点不按预期工作

The format of the call to DateTime.ParseExact is not the format of the date that you're passing. 调用DateTime.ParseExact的格式不是您传递的日期的格式。 You could change the format, or call DateTime.Parse directly as that format is recognized by it: 您可以更改格式,或直接调用DateTime.Parse,因为它可以识别该格式:

String dateString = "2018-05-01T18:30:00.000Z";

DateTime date1 = DateTime.Parse(dateString);
DateTime date2 = DateTime.ParseExact(dateString, "yyyy-MM-ddTHH:mm:ss.fffZ", null);

Console.WriteLine(date1.ToString("MMM dd yyyy"));
Console.WriteLine(date2.ToString("MMM dd yyyy"));

All you need is the following. 您只需要以下内容。 And please note that the datetime string actually represents May 1 2018, not May 5 2018 as mentioned in the question. 并且请注意,datetime字符串实际上代表2018年5月1日,而不是问题中提到的2018年5月5日。

var d = DateTime.Parse("2018-05-01T18:30:00.000Z");

Console.WriteLine(d.ToString("MMM dd yyyy"));

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

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