Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

datetime

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. 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. 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.

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?

I know if I use local time on the machine it would work OK, but I cant change the GMT time on the server. I would love to, but I cant. :-)

like image 997
H27studio Avatar asked Dec 16 '22 05:12

H27studio


1 Answers

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

I.e. DateTime object checks with system time zone, since UTC does not support daylight saving, it returns true.

If you want to verify, if the time specified fall in daylight saving for a time zone, then use IsDayLightSavingTime method in TimeZoneInfo

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);
like image 65
Manas Avatar answered Dec 18 '22 19:12

Manas