1

I'm guessing I'm missing something obvious, but here goes...

It is currently December in the mountain time zone, meaning MST and -07:00. In the following code, .NET returns a DateTime with the time portion of 1:34 PM, which correlates to -06:00. What am I doing wrong?

private void button1_Click( object sender, EventArgs e )
    {
    DateTime test = new DateTime();
    test = (DateTime)System.Convert.ChangeType( "1988-08-08T12:34:00.000-07:00", Type.GetType( "System.DateTime" ), CultureInfo.CurrentCulture );
    MessageBox.Show( test.ToString() ); // shows "8/8/1988 1:34:00 PM"
    }

I should mention that I've pulled this concept (using Convert.ChangeType) from the guts of JSON.NET, where I initially encountered the problem. I can't (and don't want to) change the code to use a different conversion method.

1 Answer 1

3

Use an offset aware struct DateTimeOffset instead of DateTime.

2
  • DateTimeOffset isn't actually time zone aware - it's just offset aware. It represents an instant in time, but with no associated time zone.
    – Jon Skeet
    Dec 2, 2010 at 20:17
  • @Jon - thanks for the correction. BTW - Does NodaTime deal with this kind of thing well?
    – Oded
    Dec 2, 2010 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.