By Fabian


2012-12-18 13:02:01 8 Comments

I need to convert UTC date strings to 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.

I already read lot of questions here on stackoverflow, but none of them solved my problem.

That is what I need to do:

Input: "2012-11-20T00:00:00Z"

Output: DateTimeOffset with:

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

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

3 comments

@Matt Johnson-Pint 2013-01-14 16:45:58

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 .LocalDateTime property - which is always going to convert the result to the local timezone of the computer. You simply need the .DateTime property instead.

@Johan Larsson 2012-12-18 13:34:28

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

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

@Fabian 2012-12-18 14:26:14

This won't work if I want to use another timezone than the current timezone.

@Johan Larsson 2012-12-18 15:09:35

@Fabian Updated my answer, hope it helps

@Fabian 2012-12-18 15:43:18

I'm not sure how this can help me. Converting the DateTime is nice and all, but how do I get a DateTimeOffset?

@VRC 2012-12-18 13:41:09

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

@Fabian 2012-12-18 14:26:42

This won't work if I want to use another timezone than the current timezone.

@VRC 2012-12-18 15:25:05

Add the second parameter, the cultureinfo. here is a list sharpertutorials.com/list-of-culture-codes. example : var fmt = new CultureInfo("en-AU").DateTimeFormat;

@Fabian 2012-12-18 15:29:13

I tried this, but no matter which CultureInfo I use the DateTimeOffset always has an offset of 00:00:00, which is not what I want!

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] DateTime vs DateTimeOffset

61 Answered Questions

[SOLVED] In C#, how do I calculate someone's age based on a DateTime type birthday?

  • 2008-07-31 23:40:59
  • Jeff Atwood
  • 628227 View
  • 1912 Score
  • 61 Answer
  • Tags:   c# .net datetime

39 Answered Questions

[SOLVED] Should I use the datetime or timestamp data type in MySQL?

10 Answered Questions

[SOLVED] Convert date to datetime in Python

17 Answered Questions

[SOLVED] Converting a String to DateTime

  • 2009-05-28 05:01:02
  • dban
  • 1372464 View
  • 605 Score
  • 17 Answer
  • Tags:   c# datetime

20 Answered Questions

[SOLVED] Converting string into datetime

  • 2009-01-21 18:00:29
  • Oli
  • 3054289 View
  • 2238 Score
  • 20 Answer
  • Tags:   python datetime

43 Answered Questions

[SOLVED] How to return only the Date from a SQL Server DateTime datatype

4 Answered Questions

[SOLVED] get DateTimeOffset from DateTime (utc) and TimeZoneInfo

  • 2011-06-08 19:29:33
  • katit
  • 39697 View
  • 18 Score
  • 4 Answer
  • Tags:   c# datetime

2 Answered Questions

[SOLVED] DateTimeOffset, not clear understanding

1 Answered Questions

[SOLVED] how to convert string to DateTime as UTC as simple as that

Sponsored Content