简体   繁体   English

有没有办法让DateTime.IsDaylightSavingTime在具有GMT / UTC时间的机器上工作?

[英]Is there a way to make DateTime.IsDaylightSavingTime work on a machine with GMT/UTC time?

I'm trying to know if there is a way to make a DateTime object get all the right properties on a OS that is set to GMT time. 我试图知道是否有办法让DateTime对象在设置为GMT时间的操作系统上获得所有正确的属性。 Not just changing time. 不只是改变时间。

Right now what im doing is: 现在我正在做的是:

DateTime f = new DateTime(2012, 8, 1); //A date on summer

DateTime f2 = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(f, TimeZoneInfo.Local.Id, "Central Europe Standard Time");

var isSummer = f2.IsDaylightSavingTime(); //It always returns false if the OS is on GMT

And it changes the hour & date to make the GMT become Central Europe Standard Time just fine, but the IsDaylightSavingTime function wont work. 它改变了小时和日期,使GMT成为中欧标准时间就好了,但IsDaylightSavingTime功能不起作用。 It always returns false because I'm using GMT on the system, but i think it shouldn't because I created a DateTime for central Europe. 它总是返回false,因为我在系统上使用GMT,但我认为不应该因为我为中欧创建了一个DateTime

Is there a way to make the DateTime object f2 really local and make it understand that if its August and is European Central it should return true instead of false? 有没有办法让DateTime对象f2真的是本地的,让它明白,如果它的八月是欧洲中心,它应该返回true而不是false?

I know if I use local time on the machine it would work OK, but I cant change the GMT time on the server. 我知道如果我在机器上使用本地时间它可以正常工作,但我无法改变服务器上的GMT时间。 I would love to, but I cant. 我很乐意,但我不能。 :-) :-)

DateTime.IsDayLightSavingTime returns true if Kind is Local or Unspecified, and the value of this instance of DateTime is within the Daylight Saving Time range for the current time zone 如果Kind为Local或Unspecified,则DateTime.IsDayLightSavingTime返回true,并且此DateTime实例的值在当前时区的夏令时范围内

Ie DateTime object checks with system time zone, since UTC does not support daylight saving, it returns true. 即DateTime对象检查系统时区,因为UTC不支持夏令时,它返回true。

If you want to verify, if the time specified fall in daylight saving for a time zone, then use IsDayLightSavingTime method in TimeZoneInfo 如果要验证,如果指定的时间属于时区的夏令时,则在TimeZoneInfo中使用IsDayLightSavingTime方法

DateTime f = new DateTime(2012, 8, 1); //A date on summer 
TimeZoneInfo tzf2=TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
DateTime f2 = TimeZoneInfo.ConvertTime(f, tzf2);
var isSummer = tzf2.IsDaylightSavingTime(f2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM