I have a date string, returned from a ExtJS datetime picker, which looks like this:
Wed Apr 25 2012 00:00:00 GMT+0300 (GTB Daylight Time)
From this I would need to have it in this format : YYYY-mm-dd, using C# or JavaScript.
How could I do this? I’ve tried using DateTime.Parse and it cannot be parsed.
Any idea?
You don’t seem to care about the time and timezone information so you can in fact use
DateTime.ParseExactto parse the date. Assuming that the day of month part may be just a single digit (e.g. 2012-04-01 isSun Apr 1 2012) the pattern you need to use isddd MMM d yyyy.The “hardest” part is really chopping of the time and timezone part. If the day of month is a single digit you have to take of substring of length 14; otherwise of length 15. Here is a way to get the index of the 4th space character in a string:
You can then parse the date into a
DateTime:This can be formatted back into a string using whatever format and culture you need.