该DateTime.Parse是关闭一小时。为什么? [英] The DateTime.Parse is off by one hour. Why?

查看:157
本文介绍了该DateTime.Parse是关闭一小时。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有一些无法理解为什么一个恢复的日期和时间字符串的值不同于原来的。我写的字符串通用日期时间(格式为U所以它有一个'Z'末),但它恢复后,就相差一小时。我使用的是U,以prevent这种东西的发生。谁能告诉我,为什么它不同于?

I've been having some trouble understanding why the value of a restored date time string differs from its original. I'm writing the string to universal datetime (format "u" so it has a 'z' at the end), but when it is restored, it differs by one hour. I'm using the "u" to prevent this kind of stuff from happening. Can anybody tell me why it differs?

我需要一个良好的字符串,再presentation,因为我将使用code在5个不同的时区。

I need a good string representation, because I'll use the code in 5 different time-zones.

该计划:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag("es-CR");

            DateTime min = DateTime.MinValue;

            Console.Write("Min value date: ");
            Console.WriteLine(min);

            Console.Write("String:         ");
            string str = min.ToString("u");
            Console.WriteLine(str);

            DateTime dt = DateTime.Parse(str);

            Console.Write("Restored Date:  ");
            Console.WriteLine(dt);

            Console.ReadLine();

        }
    }
}

的输出是:

最小值日期:01/01/0001 12:00:00 A.M

Min value date: 01/01/0001 12:00:00 a.m.

字符串:0001-01-01 00:00:00Z

String: 0001-01-01 00:00:00Z

恢复日期:01/01/0001 01:00:00 A.M

Restored Date: 01/01/0001 01:00:00 a.m.

编辑:选择尝试哥斯达黎加文化

option to try Costa Rica culture.

推荐答案

在分析通用日期时间字符串,使用本地时区?您可以使用方法ToUniversalTime()和ToLocalTime()来来回转换。此外,如果您将时区的Z,这将帮助你转换到正确的时区后所抵消。

When you parse the universal DateTime string it is using your local timezone? You can use the methods ToUniversalTime() and ToLocalTime() to convert back and forth. Also, if you place the timezone offset after the "Z" that will help you convert to the right timezone.

布莱恩

这篇关于该DateTime.Parse是关闭一小时。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆