3

I am currently trying to parse a string that is obtained from an xml that is downloaded from the web every few minutes. The string looks like this:

Thu Jul 12 08:39:56 GMT+0100 2012

At first I just did a string.split and took out everything after the time (GMT+0100 2012) and inserted 2012 after the date.

This worked great until the date changed to:

Thu Jul 12 08:39:56 GMT+0000 2012

So I would like to dynamically pasre the GMT+ whatever as they send me that string in c#.

Any advice would be appreciated.

david.s
  • 11,283
  • 6
  • 50
  • 82
Chuck Condron
  • 35
  • 1
  • 6
  • 1
    What exactly are are you trying to do with this date? Strip timezone info? convert to local time? – Arithmomaniac Jul 23 '12 at 18:46
  • parsing to local date and yes on the DateTime libraries It doesnt like the GMT+0000 it seems to only like GMT I believe, but maybe im missing something. I definately needs the 2012 after the day (July 12) – Chuck Condron Jul 23 '12 at 18:48

4 Answers4

6

You can use DateTime.ParseExact with a custom date and time format string:

DateTime.ParseExact("Thu Jul 12 08:39:56 GMT+0000 2012", 
                    "ddd MMM dd hh:mm:ss 'GMT'K yyyy",
                    CultureInfo.InvariantCulture)

This will throw a format exception if the string and format string do not match exactly, so you may want to use DateTime.TryParseExact that will return a false if it fails.

Instead of DateTime you may want to use DateTimeOffset that preserved timezone information , as @Keith commented - this may be important to your application.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • yes exactly the GMT+0000 changes to GMT+0100 at times.. if this app ran past 2012 then the 2012 would change. here is the source of the date string that I am reading:https://s3-eu-west-1.amazonaws.com/olympics-outdoor/xml/images.xml – Chuck Condron Jul 23 '12 at 18:58
  • @KeithS - Yes, I hit that issue myself when testing several different string and escaped the `GMT` to a literal. Though there is no colon, both `zzzz` and `K` deal with it just fine. – Oded Jul 23 '12 at 19:05
  • @Oded: Sweet. OK then, good answer; only other thing I'd recommend is maybe parsing it to a DateTimeOffset which preserves the time in the stated time zone instead of converting it offhand to local or UTC. – KeithS Jul 23 '12 at 19:06
  • @KeithS fair point. Will add to my answer in case it matters for the OP. – Oded Jul 23 '12 at 19:07
  • Keith, I am trying your fix, but I am not sure how to escape GMT, I put @ infront of the entire string to make it litteral but its still complaining:DateTime convertedDate = DateTime.ParseExact(datetest3, @"ddd MMM dd HH:MM:SS 'GMT'zzzz yyyy", System.Globalization.CultureInfo.InvariantCulture); – Chuck Condron Jul 23 '12 at 19:13
  • @ChuckCondron - The `GMT` _is_ escaped (putting it between single quotes makes it a literal). You need to be _very_ careful with your casing. `SS` is not valid, `ss` is. Try with `@"ddd MMM dd HH:MM:ss 'GMT'zzzz yyyy"` (I am assuming `HH` as it is 24 clock?) – Oded Jul 23 '12 at 19:14
  • also, timezone is important, im trying to detect a difference of how long ago, that date string occured. I am getting a DateTime Pattern M appears more than once with different values when I use: DateTime convertedDate = DateTime.ParseExact(datetest3, "ddd MMM dd hh:mm:ss 'GMT'K yyyy", System.Globalization.CultureInfo.InvariantCulture); – Chuck Condron Jul 23 '12 at 19:20
  • @ChuckCondron - OK. Just swap `DateTime` with `DateTimeOffset`. Also, if the hours are 24 clock, use `HH` instead of `hh`. – Oded Jul 23 '12 at 19:23
  • awesome that fixed it!: DateTimeOffset convertedDate = DateTimeOffset.ParseExact(datetest3, "ddd MMM dd hh:mm:ss 'GMT'K yyyy", System.Globalization.CultureInfo.InvariantCulture); – Chuck Condron Jul 23 '12 at 19:25
  • @ChuckCondron: One thing: the "hh" identifier for hours is parsed as a 12-hour clock, so you may get an error if you try to parse a time >= 13:00. It works with this particular example, but caveat coder. – KeithS Jul 23 '12 at 23:25
1

Two things you can do: First, you should be able to use a custom format string with a ParseExact method, either from DateTime or DateTimeOffset (I would use DateTimeOffset if the actual time zone of the stamp is important, and not just the equivalent time in UTC or your local time zone).

Have a look: DateTime custom format string

The format string would probably be something like @"ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy".

However, there's one snag; the .NET time zone offset ("zzzz" or simply "K") always includes a colon between the hour and minute when expressed as a string, which your input strings do not have. There is no way I know of to specify that the time zone offset doesn't/shouldn't have this colon, and I'm pretty sure that trying to parse it without a colon would cause an error.

The simplest workaround is to remove that specific colon from the string prior to parsing it. The code for that given your input is simply to remove the last colon character in the string:

var updatedString = inputString.Remove(inputString.LastIndexOf(':'), 1);
KeithS
  • 70,210
  • 21
  • 112
  • 164
0

Try DateTime.Parse method to parse your date.

david.s
  • 11,283
  • 6
  • 50
  • 82
  • Using the example the OP said is not working, I get: "String was not recognized as a valid DateTime." with the vanilla `DateTime.Parse`. – Oded Jul 23 '12 at 18:50
  • DateTime.Parse fails with the string given... see my comments above, 2012 needs to be after July 12, not at the end. also it seems to not like GMT+0000 anywhere int he string. – Chuck Condron Jul 23 '12 at 18:52
0

This should work:

XmlConvert.ToDateTime(textBox1.Text, "ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy");
Steen Tøttrup
  • 3,755
  • 2
  • 22
  • 36