简体   繁体   中英

How to convert the following string to date string again?

I have a string passed from kendo grid filter as follows,

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. You could change the format, or call DateTime.Parse directly as that format is recognized by it:

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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