简体   繁体   English

C#日期时间格式和存储时区信息

[英]C# datetime format and storing timezone info

I am getting strings in the form: 我得到以下形式的字符串:

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

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

I would like to store these values as DateTime objects but a simple DateTime.Parse() does not work. 我想将这些值存储为DateTime对象,但是一个简单的DateTime.Parse()不起作用。 Is there anyway I can convert those strings to DateTime objects? 无论如何,我可以将那些字符串转换为DateTime对象吗? As far as I can tell, DateTime does not know about timezones. 据我所知,DateTime不了解时区。

You can replace GMT with z and it will work: 您可以将GMT替换为z,它将起作用:

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. 对IAbstractDownvoteFactor +1-Z是最佳区域。

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). 您的日期时间看起来几乎与Iso8601类似,但具有自定义时区(有关初始信息,请参见http://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators)。

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. 如果您可以影响传入格式,请在时区中请求UTC(Z)或绝对偏移(+08:00)。

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). 否则,您需要确定需要支持的时区,确定规则是否已更改以及是否正确设置了夏时制(即在适当的时候使用PDT / PST)。 Have fun. 玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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