开发者

.NET Convert.ChangeType from string to DateTime doesn't respect timezone?

开发者 https://www.devze.com 2023-01-29 02:20 出处:网络
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

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.


Use an offset aware struct DateTimeOffset instead of DateTime.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号