7

I have a datetime in the following format: Wed, 03 September 2013 02:05:50 GMT

Now when i try to parse this string to a datetime object using a mask, i get a formatexception

DateTime parsed = DateTime.ParseExact("Wed, 03 September 2013 02:05:50 GMT", "ddd, dd MMMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture);

I did check DateTime.Now with the above mask, and it matches the date i want to parse exactly. How can i parse this date?

Stefan
  • 17,448
  • 11
  • 60
  • 79
Richard Mosselveld
  • 702
  • 1
  • 6
  • 21
  • Your Input and output are same , Why you convert it ? – Ramesh Rajendran Oct 25 '13 at 09:35
  • The above code is just a sample for my dateformat. The actual datetime is dynamic – Richard Mosselveld Oct 25 '13 at 09:37
  • check this link for datetime formatting http://www.csharp-examples.net/string-format-datetime/ – Luis Filipe Oct 25 '13 at 09:43
  • 3
    It's because 03 September was a Tuesday, not a Wednesday. – Roy Dictus Oct 25 '13 at 09:46
  • Very stupid indeed. Someone manipulated this data, but it's not a valid date. That's a part of the reason it's failing. – Richard Mosselveld Oct 25 '13 at 09:49
  • @CodeCaster, not a duplicate because here the timezone is fixed to GMT while that other question wants to parse different types of Timezones. The answer for this question does not apply to the other. – Matthijs Wessels Oct 25 '13 at 10:51
  • @Matthijs and another question isn't a duplicate because it parses a date from 2012 where here 2013 is parsed? I put as much effort in finding an exact duplicate as OP put effort in researching how to format datetimes. OP did not mention the "it's always GMT" constraint, nor show any research effort, just "It doesn't work". – CodeCaster Oct 25 '13 at 11:01
  • @CodeCaster As I mentioned, the answer to this question does not apply to the question you linked. While initially you were correct to mark it as such, it turned out this is a different scenario. Remember that it's not as much about the OP, as it is about other people finding this question and the answer to it. – Matthijs Wessels Oct 25 '13 at 11:15
  • @Matthijs it's the other way around. Using the solution in the duplicate, OP here would also have been helped. OP could at least have linked that question and say something like _"The timezone specifier always is `GMT` and I can for some reason not alter the input datetime string, so this doesn't apply"_, then the question would show more research effort and it would at least not be a duplicate of the one I linked. Granted, I learned about the `Z` parameter in Stefans answer. – CodeCaster Oct 25 '13 at 11:24
  • @CodeCaster I agree that that answer also applies here. Maybe the best solution would be to add it as an answer to this question as a link with a short summary. – Matthijs Wessels Oct 25 '13 at 11:33

2 Answers2

14

Try this:

DateTime parsed = DateTime.ParseExact("Tue, 03 September 2013 02:05:50 GMT", 
                                      "ddd, dd MMMM yyyy HH:mm:ss Z", 
                                       CultureInfo.InvariantCulture);

You should use Z for utc as "GMT". And the 3th of September was on a Tuesday.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • 2
    I wonder where this `Z` comes from. It's not [documented](http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx) and fails for any value other than `GMT`. – CodeCaster Oct 25 '13 at 09:47
  • 3
    Usage of `Z` is documented [here](http://msdn.microsoft.com/en-us/library/system.datetime.parse.aspx). This still fails on Mono though. It seems like `Z` is not implemented. – Sani Huttunen Oct 25 '13 at 09:57
  • 1
    @SaniHuttunen, CodeCaster : Basicaly I wouldn't recommend the use of `Z` at all. There are better way's to parse this string. Stripping the day and month, use actual time zone etc. But it seemed beyond the scope of the question. – Stefan Oct 25 '13 at 10:07
  • I can't change the format of the date that is being provided. For my case Z works, but it is good to know that it's not the best solution to use it. – Richard Mosselveld Oct 25 '13 at 10:42
  • 1
    `Z` most likely stands for "Zulu Time" (military slang for gmt+0 timezone). I also suggest to not use it and split up the string instead, since the format is somewhat fixed. – Alex Oct 25 '13 at 15:34
  • What about if you have a datetime in this format? 20211001142800-0500. It's in the format of "yyyyMMddHHmmss" but the "-0500" I believe means subtract 5 hours. – Mark May 25 '22 at 12:41
  • Yes, instead of `Z` you should specify the time zone format specifier. I don't know it by heard. – Stefan May 25 '22 at 15:28
2

You may try:

var input = "Tue, 03 September 2013 02:05:50 GMT";
var parsed = DateTime.ParseExact(input,
    "ddd, dd MMMM yyyy HH':'mm':'ss 'GMT'",
    CultureInfo.InvariantCulture);
Console.WriteLine(parsed);
Console.WriteLine(parsed.ToLocalTime());
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • 1
    This is also a good answer as [the documentation Sani Huttunen linked](http://msdn.microsoft.com/en-us/library/system.datetime.parse.aspx) states that either Z or GMT can be used in the format string. – Matthijs Wessels Oct 25 '13 at 11:02