假设时区将字符串解析为 DateTimeOffset
Parse String into DateTimeOffset While Assuming TimeZone
可能是一个超级简单的解决方案,但我显然遗漏了一些东西。
我有一个值为“2020/07/29 13:30:00”的字符串对象。
我如何将其解析为 DateTimeOffset 对象并假设该解析时间的时区是“GMT 标准时间”,或者我希望最好指定的任何 TimeZoneInfo?
然后我怎样才能使用那个 DateTimeOffset 和 return 它的 Utc 时间但是到我选择的任何指定时区?
非常感谢
尝试 DateTimeOffset.ParseExact overload that accepts a DateTimeStyles 参数。
此代码:
var dt=DateTimeOffset.ParseExact("2020/07/29 13:30:00","yyyy/MM/dd HH:mm:ss",
CultureInfo.InvariantCulture,DateTimeStyles.AssumeUniversal);
Returns 2020-07-29T13:30:00.0000000+00:00
没有 GMT Standard Time
,这是 Windows 中使用的一个非常不幸的名称,它以某种方式设法混淆了英国时间和 UTC 时间,以至于没有人不看就知道它的意思文档。在这个问题中对此进行了彻底的讨论和解释:Difference between UTC and GMT Standard Time in .NET。正如其中一个答案所解释的那样:
The names GMT Standard Time and GMT Daylight Time are unknown outside of Redmond. They are mythical animals that appear only in the bestiary called the Windows Registry.
如果您想假定英国时间并且您的机器使用英国时区,您可以使用 DateTimeStyles.AssumeLocal
我能找到的最简单的是这样的。
我找不到在特定给定时区解析 DateTimeOffset
的任何方法,但您可以将字符串解析为 DateTime
(Kind
为 Unspecified
,它只是充当字符串中信息位的容器,而不尝试对其应用时区知识。
然后你可以向 TimeZoneInfo
询问给定本地时间给定时区的 UTC
偏移量,并将其应用于 DateTime
以创建 DateTimeOffset
.
获得 DateTimeOffset
后,您可以使用它的 ToOffset
方法和 TimeZoneInfo.ConvertTime
.
string input = "2020/07/29 13:30:00";
var timezone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
// DateTime.Parse creates a DateTime with Kind == Unspecified
var dateTime = DateTime.Parse(input);
Console.WriteLine(dateTime); // 7/29/2020 1:30:00 PM
// Since Kind == Unspecified, timezone.GetUtcOffset will give us the UTC offset in effect at
// the given local time in timezone
var dateTimeOffset = new DateTimeOffset(dateTime, timezone.GetUtcOffset(dateTime));
Console.WriteLine(dateTimeOffset); // 7/29/2020 1:30:00 PM +01:00
// Convert to UTC
Console.WriteLine(dateTimeOffset.UtcDateTime); // 7/29/2020 12:30:00 PM
Console.WriteLine(dateTimeOffset.ToOffset(TimeSpan.Zero)); // 7/29/2020 12:30:00 PM +00:00
// Convert to another timezone
var cst = TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time");
Console.WriteLine(TimeZoneInfo.ConvertTime(dateTimeOffset, cst)); // 7/29/2020 6:30:00 AM -06:00
此函数应将您的日期时间字符串(假设这是 GMT 标准时间)转换为任何其他时区:
public static DateTime? ToUTCTimeZone(string sDate, string timeZone)
{
DateTime utcDate = DateTime.Parse(sDate);
DateTimeOffset localServerTime = DateTimeOffset.Now;
utcDate = DateTime.SpecifyKind(utcDate, DateTimeKind.Utc);
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
if (cstZone == null)
return null;
return TimeZoneInfo.ConvertTimeFromUtc(utcDate, cstZone);
}//ToUTCTimeZone
可能是一个超级简单的解决方案,但我显然遗漏了一些东西。
我有一个值为“2020/07/29 13:30:00”的字符串对象。
我如何将其解析为 DateTimeOffset 对象并假设该解析时间的时区是“GMT 标准时间”,或者我希望最好指定的任何 TimeZoneInfo?
然后我怎样才能使用那个 DateTimeOffset 和 return 它的 Utc 时间但是到我选择的任何指定时区?
非常感谢
尝试 DateTimeOffset.ParseExact overload that accepts a DateTimeStyles 参数。
此代码:
var dt=DateTimeOffset.ParseExact("2020/07/29 13:30:00","yyyy/MM/dd HH:mm:ss",
CultureInfo.InvariantCulture,DateTimeStyles.AssumeUniversal);
Returns 2020-07-29T13:30:00.0000000+00:00
没有 GMT Standard Time
,这是 Windows 中使用的一个非常不幸的名称,它以某种方式设法混淆了英国时间和 UTC 时间,以至于没有人不看就知道它的意思文档。在这个问题中对此进行了彻底的讨论和解释:Difference between UTC and GMT Standard Time in .NET。正如其中一个答案所解释的那样:
The names GMT Standard Time and GMT Daylight Time are unknown outside of Redmond. They are mythical animals that appear only in the bestiary called the Windows Registry.
如果您想假定英国时间并且您的机器使用英国时区,您可以使用 DateTimeStyles.AssumeLocal
我能找到的最简单的是这样的。
我找不到在特定给定时区解析 DateTimeOffset
的任何方法,但您可以将字符串解析为 DateTime
(Kind
为 Unspecified
,它只是充当字符串中信息位的容器,而不尝试对其应用时区知识。
然后你可以向 TimeZoneInfo
询问给定本地时间给定时区的 UTC
偏移量,并将其应用于 DateTime
以创建 DateTimeOffset
.
获得 DateTimeOffset
后,您可以使用它的 ToOffset
方法和 TimeZoneInfo.ConvertTime
.
string input = "2020/07/29 13:30:00";
var timezone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
// DateTime.Parse creates a DateTime with Kind == Unspecified
var dateTime = DateTime.Parse(input);
Console.WriteLine(dateTime); // 7/29/2020 1:30:00 PM
// Since Kind == Unspecified, timezone.GetUtcOffset will give us the UTC offset in effect at
// the given local time in timezone
var dateTimeOffset = new DateTimeOffset(dateTime, timezone.GetUtcOffset(dateTime));
Console.WriteLine(dateTimeOffset); // 7/29/2020 1:30:00 PM +01:00
// Convert to UTC
Console.WriteLine(dateTimeOffset.UtcDateTime); // 7/29/2020 12:30:00 PM
Console.WriteLine(dateTimeOffset.ToOffset(TimeSpan.Zero)); // 7/29/2020 12:30:00 PM +00:00
// Convert to another timezone
var cst = TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time");
Console.WriteLine(TimeZoneInfo.ConvertTime(dateTimeOffset, cst)); // 7/29/2020 6:30:00 AM -06:00
此函数应将您的日期时间字符串(假设这是 GMT 标准时间)转换为任何其他时区:
public static DateTime? ToUTCTimeZone(string sDate, string timeZone)
{
DateTime utcDate = DateTime.Parse(sDate);
DateTimeOffset localServerTime = DateTimeOffset.Now;
utcDate = DateTime.SpecifyKind(utcDate, DateTimeKind.Utc);
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
if (cstZone == null)
return null;
return TimeZoneInfo.ConvertTimeFromUtc(utcDate, cstZone);
}//ToUTCTimeZone