2012-12-18 256 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

相關問題