Phoenix
Phoenix

Reputation: 467

How To Add UTC Offset to current date

at the moment I need to determine what timezone is currently "9am". I have a table to timezones and their offsets plus taking into consideration daylight savings

The offsets are stored in my database as +/- time.

For example, LA/Vancouver is -8:00

The approach I was going to take was to iterate through all time zones, convert the offset so I can addhours and addminutes to DateTime.UtcNow then check if the hour is 9. The only issue I see is that it's not able to handle daylight savings.

I was wondering is there are any existing libraries I can leverage for my scenario or a better approach to the one I suggested? In the end I require the ID column.

Timezones table

enter image description here

Upvotes: 0

Views: 1686

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156554

This is relatively easy to do using the built-in .NET classes TimeZoneInfo and DateTime, but you might be surprised by the results.

from z in TimeZoneInfo.GetSystemTimeZones()
let timeThere = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, z)
where timeThere.Hour == 9
select new{z.DisplayName, timeThere}

For example, right now there are 7 time zones that my machine is aware of in which it's in the 9 AM hour.

enter image description here

Upvotes: 1

Related Questions