Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将UTC/GMT时间转换为本地时间_C#_.net_Datetime_Utc - Fatal编程技术网

C# 将UTC/GMT时间转换为本地时间

C# 将UTC/GMT时间转换为本地时间,c#,.net,datetime,utc,C#,.net,Datetime,Utc,我们正在为web服务客户端开发一个C#应用程序。这将在Windows XP PC上运行 web服务返回的字段之一是DateTime字段。服务器返回一个GMT格式的字段,即在末尾带有“Z” 然而,我们发现.NET似乎做了某种隐式转换,时间总是12小时 下面的代码示例在某种程度上解决了这一问题,因为12小时的差异已经消失,但它没有考虑新西兰夏令时 CultureInfo ci = new CultureInfo("en-NZ"); string date = "Web service date".T

我们正在为web服务客户端开发一个C#应用程序。这将在Windows XP PC上运行

web服务返回的字段之一是DateTime字段。服务器返回一个GMT格式的字段,即在末尾带有“Z”

然而,我们发现.NET似乎做了某种隐式转换,时间总是12小时

下面的代码示例在某种程度上解决了这一问题,因为12小时的差异已经消失,但它没有考虑新西兰夏令时

CultureInfo ci = new CultureInfo("en-NZ");
string date = "Web service date".ToString("R", ci);
DateTime convertedDate = DateTime.Parse(date);            
根据:

UTC/GMT偏移量

标准时区:UTC/GMT+12小时
夏令时:+1小时
当前时区偏移:UTC/GMT+13小时


我们如何适应额外的一小时?这可以通过编程实现吗?或者这是电脑上的某种设置吗?

回答Dana的建议:

TimeZone.CurrentTimeZone.ToLocalTime(date);
代码示例现在看起来像:

string date = "Web service date"..ToString("R", ci);
DateTime convertedDate = DateTime.Parse(date);            
DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(convertedDate);
最初的日期是2008年8月20日;那种是UTC

“convertedDate”和“dt”都是相同的:


21/08/08 10:00:26;这种类型是本地的

我遇到了一个问题,它位于一个跨线路推送的数据集中(webservice到客户端),它会自动更改,因为DataColumn的DateType字段设置为本地。如果将数据集推送到其他位置,请确保检查日期类型


如果不想更改,请将其设置为“未指定”

我只想添加一个一般注意事项

如果您所做的只是从计算机的内部时钟获取当前时间,以便在显示器或报告上显示日期/时间,那么一切正常。但是,如果您正在保存日期/时间信息以供以后参考,或者正在计算日期/时间,请小心

假设您确定一艘游轮于2007年12月20日UTC 15:00抵达檀香山。你想知道那是当地时间。
1.可能至少有三个“本地人”参与其中。Local可能表示檀香山,也可能表示您的计算机所在地,也可能表示您的客户所在地。
2.如果使用内置函数进行转换,则可能是错误的。这是因为夏令时(可能)目前在您的计算机上生效,但在12月未生效。但Windows不知道这一点。。。它只有一个标志来确定夏令时当前是否有效。如果它目前生效,那么它将很高兴地在12月的日期上增加一个小时。

3.夏令时在不同的政治分区有不同的实施方式(或者根本没有)。不要认为仅仅因为您的国家/地区在特定日期发生变化,其他国家/地区也会发生变化。

不要忘记,如果您已经有一个DateTime对象,并且不确定它是UTC还是本地对象,那么直接使用对象上的方法就足够简单了:

DateTime convertedDate = DateTime.Parse(date);
DateTime localDate = convertedDate.ToLocalTime();
我们如何适应额外的一小时

除非指定,否则.net将使用本地pc设置。我想读一读:

从外观上看,代码可能类似于:

DaylightTime daylight = TimeZone.CurrentTimeZone.GetDaylightChanges( year );

如上所述,再次检查服务器的时区设置。网上有一些文章介绍如何安全地影响IIS中的更改。

如果您使用的是.net 3.5,我会研究使用System.TimeZoneInfo类。看见这应该正确考虑夏令时的变化

// Coordinated Universal Time string from 
// DateTime.Now.ToUniversalTime().ToString("u");
string date = "2009-02-25 16:13:00Z"; 
// Local .NET timeZone.
DateTime localDateTime = DateTime.Parse(date); 
DateTime utcDateTime = localDateTime.ToUniversalTime();

// ID from: 
// "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zone"
// See http://msdn.microsoft.com/en-us/library/system.timezoneinfo.id.aspx
string nzTimeZoneKey = "New Zealand Standard Time";
TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(nzTimeZoneKey);
DateTime nzDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, nzTimeZone);

对于字符串,如
2012-09-19 01:27:30.000
DateTime.Parse
无法分辨日期和时间来自哪个时区

DateTime
有一个种类属性,可以有三个时区选项之一:

  • 未指明
  • 本地的
  • Utc
注意如果您希望表示UTC或本地时区以外的日期/时间,则应使用


因此,对于您问题中的代码:

DateTime convertedDate = DateTime.Parse(dateStr);

var kind = convertedDate.Kind; // will equal DateTimeKind.Unspecified
你说你知道它是什么样的,所以告诉它

DateTime convertedDate = DateTime.SpecifyKind(
    DateTime.Parse(dateStr),
    DateTimeKind.Utc);

var kind = convertedDate.Kind; // will equal DateTimeKind.Utc
现在,一旦系统知道UTC时间,您可以调用
ToLocalTime

DateTime dt = convertedDate.ToLocalTime();

这将为您提供所需的结果。

我遇到了这个问题,因为我对您通过twitter API(在状态字段中创建)返回的UTC日期有问题;我需要把它们转换成DateTime。本页答案中的任何答案/代码示例都不足以阻止我获得“字符串未被识别为有效日期时间”错误(但这是我找到正确答案的最近一次)


在这里发布此链接以防对其他人有所帮助-我需要的答案在这篇博文中找到:-基本上使用DateTime.ParseExact,使用格式字符串而不是DateTime.Parse

我知道这是一个较老的问题,但我遇到了类似的情况,我想与未来的搜索者分享我的发现,可能包括我自己:)

DateTime.Parse()
可能很棘手——例如,请参见

如果<代码> DATETIME//COD>来自Web服务或其他具有已知格式的源,您可能需要考虑一些类似

的内容。
DateTime.ParseExact(dateString, 
                   "MM/dd/yyyy HH:mm:ss", 
                   CultureInfo.InvariantCulture, 
                   DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)
或者,甚至更好

DateTime.TryParseExact(...)
AssumeUniversal
标志告诉解析器日期/时间已经是UTC;
AssumeUniversal
AdjustToUniversal
的组合告诉它不要将结果转换为“本地”时间,默认情况下它会尝试这样做。(我个人尝试在业务/应用程序/服务层专门与UTC打交道。但绕过转换到本地时间也会加快速度——在我的测试中,速度会提高50%或更多,见下文。)

以下是我们之前所做的:

DateTime.Parse(dateString, new CultureInfo("en-US"))
我们分析了这个应用程序,发现DateTime.Parse占CPU使用量的很大一部分。(顺便提一下,
CultureInfo
构造函数对CPU使用率没有显著影响。)

因此,我设置了一个控制台应用程序,以各种方式解析日期/时间字符串10000次。底线:
Parse()
10秒
ParseExact()convertedDate.ToLocalTime();
@TimeZoneInfo.ConvertTimeFromUtc(timeUtc, TimeZoneInfo.Local)
CreatedDate.ToUniversalTime().ToLocalTime();