我正在尝试解析类似于的国际日期时间字符串:
24-okt-08 21:09:06 CEST
到目前为止,我有类似的东西:
CultureInfo culture = CultureInfo.CreateSpecificCulture("nl-BE"); DateTime dt = DateTime.ParseExact("24-okt-08 21:09:06 CEST", "dd-MMM-yy HH:mm:ss ...", culture);
问题是我应该在格式字符串中使用'...'?查看自定义日期和时间格式字符串 MSDN页面似乎没有列出用于以PST/CEST/GMT/UTC格式解析时区的格式字符串.
AFAIK时区缩写无法识别.但是,如果将缩写替换为时区偏移量,则可以.例如:
DateTime dt1 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+2"), "dd-MMM-yy HH:mm:ss z", culture); DateTime dt2 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02"), "dd-MMM-yy HH:mm:ss zz", culture); DateTime dt3 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02:00"), "dd-MMM-yy HH:mm:ss zzz", culture);
答案很简单,你不能这样做.
这就是原因,
有一个世界时区的权威数据库,您可以在这里从IANA获取.
问题是,3或4个字母的缩写与IANA时区有多对一的关联.例如"AMT"
,根据您的文化,您所处的世界的哪个部分以及您的应用环境,意味着不同的东西.
AMT "Armenia Time" Asia UTC + 4 hours AMT "Amazon Time" South America UTC - 4 hours
如果你真的想解决这个问题,我建议用Noda Time代表你Instance
的.您必须编写一些代码才能将缩写转换为标准的IANA时区.
我们无法为您执行此操作,具体取决于您的应用程序的上下文.
另一个好例子是"CST"
.
CST "China Standard Time" Asia UTC + 8 hours CST "Central Standard Time" Central America UTC - 6 hours CST "Cuba Standard Time" Caribbean UTC - 5 hours CST "Central Standard Time" North America UTC - 6 hours
缩写词典,如果你决定去搜索和替换路线(我做了).
Dictionary_timeZones = new Dictionary () { {"ACDT", "+1030"}, {"ACST", "+0930"}, {"ADT", "-0300"}, {"AEDT", "+1100"}, {"AEST", "+1000"}, {"AHDT", "-0900"}, {"AHST", "-1000"}, {"AST", "-0400"}, {"AT", "-0200"}, {"AWDT", "+0900"}, {"AWST", "+0800"}, {"BAT", "+0300"}, {"BDST", "+0200"}, {"BET", "-1100"}, {"BST", "-0300"}, {"BT", "+0300"}, {"BZT2", "-0300"}, {"CADT", "+1030"}, {"CAST", "+0930"}, {"CAT", "-1000"}, {"CCT", "+0800"}, {"CDT", "-0500"}, {"CED", "+0200"}, {"CET", "+0100"}, {"CEST", "+0200"}, {"CST", "-0600"}, {"EAST", "+1000"}, {"EDT", "-0400"}, {"EED", "+0300"}, {"EET", "+0200"}, {"EEST", "+0300"}, {"EST", "-0500"}, {"FST", "+0200"}, {"FWT", "+0100"}, {"GMT", "GMT"}, {"GST", "+1000"}, {"HDT", "-0900"}, {"HST", "-1000"}, {"IDLE", "+1200"}, {"IDLW", "-1200"}, {"IST", "+0530"}, {"IT", "+0330"}, {"JST", "+0900"}, {"JT", "+0700"}, {"MDT", "-0600"}, {"MED", "+0200"}, {"MET", "+0100"}, {"MEST", "+0200"}, {"MEWT", "+0100"}, {"MST", "-0700"}, {"MT", "+0800"}, {"NDT", "-0230"}, {"NFT", "-0330"}, {"NT", "-1100"}, {"NST", "+0630"}, {"NZ", "+1100"}, {"NZST", "+1200"}, {"NZDT", "+1300"}, {"NZT", "+1200"}, {"PDT", "-0700"}, {"PST", "-0800"}, {"ROK", "+0900"}, {"SAD", "+1000"}, {"SAST", "+0900"}, {"SAT", "+0900"}, {"SDT", "+1000"}, {"SST", "+0200"}, {"SWT", "+0100"}, {"USZ3", "+0400"}, {"USZ4", "+0500"}, {"USZ5", "+0600"}, {"USZ6", "+0700"}, {"UT", "-0000"}, {"UTC", "-0000"}, {"UZ10", "+1100"}, {"WAT", "-0100"}, {"WET", "-0000"}, {"WST", "+0800"}, {"YDT", "-0800"}, {"YST", "-0900"}, {"ZP4", "+0400"}, {"ZP5", "+0500"}, {"ZP6", "+0600"} };