我们正在为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小时
我们如何调整额外的一小时?这可以通过编程来完成吗?或者这是PC上的某种设置吗?
我只是想补充一个一般性的警告。
如果你所做的只是从计算机内部时钟获取当前时间,然后将日期/时间显示在显示器或报告上,那么一切都很好。但是如果您正在保存日期/时间信息以供以后参考,或者正在计算日期/时间,请小心!
Let's say you determine that a cruise ship arrived in Honolulu on 20 Dec 2007 at 15:00 UTC. And you want to know what local time that was.
1. There are probably at least three 'locals' involved. Local may mean Honolulu, or it may mean where your computer is located, or it may mean the location where your customer is located.
2. If you use the built-in functions to do the conversion, it will probably be wrong. This is because daylight savings time is (probably) currently in effect on your computer, but was NOT in effect in December. But Windows does not know this... all it has is one flag to determine if daylight savings time is currently in effect. And if it is currently in effect, then it will happily add an hour even to a date in December.
3. Daylight savings time is implemented differently (or not at all) in various political subdivisions. Don't think that just because your country changes on a specific date, that other countries will too.
对于诸如2012-09-19 01:27:30.000,DateTime. 000等字符串。解析无法判断日期和时间来自哪个时区。
DateTime有一个Kind属性,它可以有三个时区选项之一:
未指明的
当地的
Utc
如果您希望表示UTC或本地时区以外的日期/时间,那么您应该使用DateTimeOffset。
对于你问题中的代码
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();
这将给你你需要的结果。