我需要将UTC
日期字符串转换为DateTimeOffsets
。将UTC日期时间转换为日期时间偏移
这必须使用与计算机时区不同的时区。 E.g.当前计算机时区为+02:00,但我想创建一个偏移量为-4:00的DateTimeOffset。
我已经阅读了很多关于stackoverflow的问题,但是他们没有解决我的问题。
这就是我需要做的:
输入: “2012-11-20T00:00:00Z”
输出:的DateTimeOffset有:
- 的UtcDateTime 2012-11-20 00:00
- 定义的时区的正确Utc偏移量(在此前的01:00)充足)
- LocalDateTime:二○一二年十一月二十○日01:00(= UtcDateTime +偏移)
当然夏令必须加以考虑。
编辑: 为了使事情更加清楚,请尽量填写下面的代码片段:
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));
谢谢,这似乎工作正常! – Fabian