2012-12-18 255 views
13

我需要将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)); 

回答

9

这里是解决方案,您正在寻找:

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)); 

请注意,您错误地测试了.LocalDateTime属性 - 这是始终将结果转换为计算机的本地时区。您只需要.DateTime属性。

+0

谢谢,这似乎工作正常! – Fabian

3

这是你想要的东西:

[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); 
} 
  • 注意,我断言在瑞典有效(CET)
  • 有几个重载

是您转换这个有用:

[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); 
} 
+0

如果我想使用除当前时区以外的其他时区,这将不起作用。 – Fabian

+0

@Fabian更新了我的答案,希望它有帮助 –

+0

我不知道这可以帮助我。转换日期时间是很好的和所有,但我怎么得到一个DateTimeOffset? – Fabian

-1
const String dateString = "2012-11-20T00:00:00Z"; 
var offsetDate = DateTimeOffset.Parse(dateString); 
var offsetDate2 = DateTime.Parse(dateString); 

输出为

offsetDate {20-11-2012 0:00:00 +00:00} System.DateTimeOffset 
offsetDate2 {20-11-2012 1:00:00}   System.DateTime 
+0

如果我想使用除当前时区以外的其他时区,则这不起作用。 – Fabian

+0

添加第二个参数cultureinfo。这里是一个清单http://sharpertutorials.com/list-of-culture-codes/。例如:var fmt = new CultureInfo(“en-AU”)。DateTimeFormat; – VRC

+0

我试过这个,但无论哪个CultureInfo我使用DateTimeOffset总是有一个00:00:00的偏移量,这不是我想要的! – Fabian

相关问题