【问题标题】:C# DateTime.TryParseExact getting the timezone wrongC# DateTime.TryParseExact 获取时区错误
【发布时间】:2014-05-09 18:20:32
【问题描述】:

我正在尝试解析包含一些时区信息的日期时间字符串(美国东部标准时间 2014 年 5 月 1 日晚上 9:45):

DateTime RecordedDateTime;
string fixedRecordedDateTime = "Thu May 01 21:45:00 +0500 2014";
string[] dateFormats = { "ddd MMM dd HH:mm:ss zzz yyyy" , "ddd MMM dd HH:mm:ss K yyyy" };
DateTime.TryParseExact(fixedRecordedDateTime, dateFormats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out RecordedDateTime);

DateTime 对象获取当地时间 2014 年 5 月 1 日下午 12:45(我在美国东部时间)

如果我使用(太平洋标准时间 2014 年 5 月 1 日晚上 9:45):

string fixedRecordedDateTime = "Thu May 01 21:45:00 +0800 2014";

DateTime 对象获取当地时间 2014 年 5 月 1 日上午 9:45

我不明白出了什么问题,为什么我在提供时区信息后无法更正当地时间。

我正在尝试使用 DateTime 而不是 DateTimeOffset,有什么方法可以正确解析它吗?

【问题讨论】:

  • EST 是 GMT-5。 PST 是 GMT-8。 GMT+5 是印度巴基斯坦,GMT+8 是中国。
  • 附带说明,请查看Noda Time
  • @AntonTykhyy GMT+5.30 是印度,而不是 GMT+5

标签: c# datetime timezone tryparse


【解决方案1】:

string fixedRecordedDateTime = "Thu May 01 21:45:00 +0500 2014";

这是 UTC+0500 的 21:45。 UTC 时间为 16:45,美国东部时间 (UTC-0400) 时间为 12:45。我认为您的 + 和 - 在偏移量中颠倒了。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-08-04
  • 2020-09-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多