1

I am getting strings in the form:

"2011-10-12T11:55:34.803EST"

"2011-10-05T16:58:05.043GMT"

I would like to store these values as DateTime objects but a simple DateTime.Parse() does not work. Is there anyway I can convert those strings to DateTime objects? As far as I can tell, DateTime does not know about timezones.

Flack
  • 5,727
  • 14
  • 69
  • 104
  • 1
    This post may be helpful: http://stackoverflow.com/questions/179940/c-convert-utc-gmt-time-to-local-time – George Johnston Oct 12 '11 at 18:42
  • Check this post http://stackoverflow.com/questions/241789/parse-datetime-with-timezone-of-form-pst-cest-utc-etc – Emaad Ali Oct 12 '11 at 18:47
  • 1
    Why are you trying to store times based out of different time zones. You should store a time and date in a single time zone IF your going to be working with the data. If your going to simply display it, then just store it exactly like you have it, and display it the same way. – Security Hound Oct 12 '11 at 19:02

2 Answers2

1

You can replace GMT with z and it will work:

string date = "2011-10-05T16:58:05.043GMT".Replace("GMT", "z");
Console.WriteLine(DateTime.Parse(date));

Zulu time

Joe
  • 80,724
  • 18
  • 127
  • 145
  • Thanks IAbstractDownvoteFactor. How about for times using "EST"? – Flack Oct 12 '11 at 18:48
  • That's troublesome, because EST means nothing to .NET. You might need a lookup table. Technically you can replace EST with -500 and it will work though. So if you only have GMT and EST I suppose that's reasonable – Joe Oct 12 '11 at 18:57
0

+1 to IAbstractDownvoteFactor - Z is the best zone.

Your date time look almost like Iso8601, but with custom time zones (http://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators for initial information).

Working with time zones is very hard as rules for them may change and some can appear and disappear. If you can influence incoming format - ask for UTC (Z) or absolute offset (+08:00) in time zones.

Otherwise you need to figure out what time zone you need to support, figure out if rules ever changed and if daylight saving zones are set correctly (i.e. PDT/PST used when appropriate). Have fun.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179