-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?

3
  • 1
    -1 because not a lot of effort has gone in to finding an answer prior to posting on SO Commented Jan 22, 2014 at 11:01
  • StackOverflow has a wonderful feature: Search. Use it! Commented Jan 22, 2014 at 11:02
  • @Dineshkumar I rolled back your changes. Please don't edit "Possible duplicate" into the body of a post. Thank you. Commented Jan 22, 2014 at 11:07

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.