-1

How to get DateTime from string: (I don't know what format it is)

Wed Jan 08 2014 00:00:00 GMT+0100 (Central Europe Standard Time)

To DateTime?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Milos
  • 647
  • 6
  • 21

2 Answers2

2

Using DateTime.TryParseExact to parse the string into DateTime Object.

You can pass the format according to which you want parsing. Like in your case you would use

string dateString = "Wed Jan 08 2014 00:00:00 GMT+0100";
dateString = dateString.Replace("GMT+0100", "+01:00");  // to remove the timezone abbreviation and replace with offset value.

string format = "ddd MMM dd yyyy HH:mm:ss zzz";
DateTime dt;
DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
Sachin
  • 40,216
  • 7
  • 90
  • 102
0

If you don't know what format it is, then how can you expect to parse it? How will you know if the date 2014-01-02 means the 1st Feb or 2nd Jan?

You could use a number of TryParseExact, just to rule out dates it definitely couldn't be (2014-13-12 can only be 13th Dec) but you're still left with those strings that could be parsed and are valid for two dates.

Mashton
  • 6,037
  • 2
  • 25
  • 35