C# 将 UTC DateTime 转换为 DateTimeOffset
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13933654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert UTC DateTime to DateTimeOffset
提问by Fabian
I need to convert UTCdate strings to DateTimeOffsets.
我需要将UTC日期字符串转换为DateTimeOffsets.
This must work with a timezone which differs from the computers timezone.E.g. current computer timezone is +02:00, but I want to create a DateTimeOffset with offset -4:00.
这必须适用于与计算机时区不同的时区。例如,当前计算机时区是 +02:00,但我想创建一个偏移量为 -4:00 的 DateTimeOffset。
I already read lot of questions here on stackoverflow, but none of them solved my problem.
我已经在 stackoverflow 上阅读了很多问题,但没有一个能解决我的问题。
That is what I need to do:
这就是我需要做的:
Input:"2012-11-20T00:00:00Z"
输入:“2012-11-20T00:00:00Z”
Output:DateTimeOffset with:
输出:DateTimeOffset 与:
- UtcDateTime of 2012-11-20 00:00
- the correct Utc offset for the defined timezone (01:00 in this example)
- LocalDateTime: 2012-11-20 01:00(= UtcDateTime + Offset)
- UtcDateTime of 2012-11-20 00:00
- 定义时区的正确 Utc 偏移量(本例中为 01:00)
- LocalDateTime: 2012-11-20 01:00(= UtcDateTime + Offset)
Of course daylight saving must be taken into account.
当然,必须考虑夏令时。
edit: To make things even clearer, please try to complete the following code snippet:
编辑:为了使事情更清楚,请尝试完成以下代码片段:
DateTimeOffset result;
const string dateString = "2012-11-20T00:00:00Z";
var timezone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); //this timezone has an offset of +01:00:00 on this date
//do conversion here
Assert.AreEqual(result.Offset, new TimeSpan(1, 0, 0)); //the correct utc offset, in this case +01:00:00
Assert.AreEqual(result.UtcDateTime, new DateTime(2012, 11, 20, 0, 0, 0)); //equals the original date
Assert.AreEqual(result.LocalDateTime, new DateTime(2012, 11, 20, 1, 0, 0));
采纳答案by Matt Johnson-Pint
Here is the solution you are looking for:
这是您正在寻找的解决方案:
const string dateString = "2012-11-20T00:00:00Z";
var timezone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); //this timezone has an offset of +01:00:00 on this date
var utc = DateTimeOffset.Parse(dateString);
var result = TimeZoneInfo.ConvertTime(utc, timezone);
Assert.AreEqual(result.Offset, new TimeSpan(1, 0, 0)); //the correct utc offset, in this case +01:00:00
Assert.AreEqual(result.UtcDateTime, new DateTime(2012, 11, 20, 0, 0, 0)); //equals the original date
Assert.AreEqual(result.DateTime, new DateTime(2012, 11, 20, 1, 0, 0));
Note that you were incorrectly testing the .LocalDateTimeproperty - which is alwaysgoing to convert the result to the local timezone of the computer. You simply need the .DateTimeproperty instead.
请注意,您错误地测试了该.LocalDateTime属性 - 它总是会将结果转换为计算机的本地时区。您只需要该.DateTime属性即可。
回答by Johan Larsson
Is this what you want:
这是你想要的吗:
[Test]
public void ParseUtcDateTimeTest()
{
DateTime dateTime = DateTime.Parse("2012-11-20T00:00:00Z");
Assert.AreEqual(new DateTime(2012, 11, 20, 01, 00, 00), dateTime);
DateTimeOffset dateTimeOffset = new DateTimeOffset(dateTime);
Assert.AreEqual(new TimeSpan(0, 1, 0, 0), dateTimeOffset.Offset);
}
- Note that my asserts are valid in Sweden (CET)
- There are a couple of overloads on
DateTime.Parse()
- 请注意,我的断言在瑞典(CET)有效
- 有几个重载
DateTime.Parse()
Is this useful for your conversion:
这对您的转换有用吗:
[Test]
public void ConvertTimeTest()
{
? ? DateTime dateTime = DateTime.Parse("2012-11-20T00:00:00Z");
? ? TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
? ? DateTime convertedTime = TimeZoneInfo.ConvertTime(dateTime, cstZone);
? ? Assert.AreEqual(new DateTime(2012, 11, 19, 18, 00, 00), convertedTime);
? ? TimeSpan baseUtcOffset = cstZone.BaseUtcOffset;
? ? Assert.AreEqual(new TimeSpan(0, -6, 0, 0), baseUtcOffset);
}
回答by VRC
const String dateString = "2012-11-20T00:00:00Z";
var offsetDate = DateTimeOffset.Parse(dateString);
var offsetDate2 = DateTime.Parse(dateString);
Output is
输出是
offsetDate {20-11-2012 0:00:00 +00:00} System.DateTimeOffset
offsetDate2 {20-11-2012 1:00:00} System.DateTime

