简体   繁体   中英

C# datetime format and storing timezone info

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.

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

+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 (ie PDT/PST used when appropriate). Have fun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM