Reputation: 2205
My Windows web application server is set at UTC so there is no indication of DST or not on the timezone selection UI. So when my c# code is running on that server, DateTime.Now.IsDaylightSavingTime()
always returns False. Is there a way to check to see if a particular time zone (i.e. Eastern Time) is currently in DST or not?
thanks.
Upvotes: 0
Views: 388
Reputation: 241959
DateTime.Now
is evil, and should never be used in server-side code. Read more in my blog post on this subject: The Case Against DateTime.Now. With careful coding, your code should not care what the time zone setting of the server is.
You are looking for this:
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
bool dst = tz.IsDaylightSavingTime(DateTime.UtcNow);
Don't let the string "Eastern Standard Time"
fool you - that is the TimeZoneInfo.Id
for the US Eastern time zone, encompassing both Eastern Standard Time and Eastern Daylight Time.
Upvotes: 2
Reputation: 5692
You can get a list of all time zones with TimeZoneInfo.GetSystemTimeZones()
. It returns a collection of type TimeZoneInfo
. When you find the right one, it has a property SupportsDaylightSavingTime
to tell you if it uses DST at all.
After that, you can call IsDaylightSavingTime(DateTime)
to see if DST is in effect for a given date.
Upvotes: 1