Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert somebody's local time to the UTC time

i'm a little lost in the timezone :)

I have data stored with the time in UTC. The server is in the Netherlands, so we live in utc+1 (now, with daylightsavingtime, in utc + 2)

Now a client says: give me the data from august 5th.

So i have to calculate the utc time from 'his time'. For that i have to know:

what is your utc offset (we stored that in his profile, let's say utc -6) are you in daylightsavingtime (because then we have to add +1 and make the utc offset -5)

Then my questions:

  1. Can i ask the .Net framework: does country XX have daylightsavingtime?

  2. Can i ask the .Net framework: is 08-05-2010T00:00:00 in country XXX daylightsavingtime at that moment?

i've been trying the .ToLocalTime(), but this only gives me the local time at the server, and that's not what i want, i want to calculate with the timezone of the user, and also with the fact that at that particular point in time, if he/she is in daylightsavingtime

I've also seen this VB example:

TimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time") 
Dim Dated As DateTime = TimeZoneInfo.ConvertTimeToUtc(TempDate, TimeZone) 

but imho this doesn't take into account that the user in this timezone is or is not in a daylightsavingtime (dst) country. For instance, one user in this timezone is in the netherlands having dst, and one other is in another country which has no dst.

like image 225
Michel Avatar asked Aug 05 '10 07:08

Michel


People also ask

How do you calculate UTC time from local time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.).

Is UTC 24-hour or 12-hour?

UTC uses 24-hour (military) time notation and is based on the local standard time on the 0° longitude meridian which runs through Greenwich, England.

What is UTC time now in USA?

UTC current time is 13:31:38 UTC current date is 14th Friday October 2022.

How do you convert date to UTC format?

var now = new Date(); var utc = new Date(now. getTime() + now. getTimezoneOffset() * 60000);


2 Answers

You can't ask the framework about a particular country - but you can ask about a particular time zone.

TimeZoneInfo does take DST into account. (Heck, it wouldn't have the IsDaylightSavingTime method otherwise.) If you've got two users, one of whom is currently observing DST and the other of whom isn't, then they aren't in the same time zone.

If you could specify which locations you're talking about, I could try to find out which time zones are involved. (It's generally easier to find out the Olson names, but it shouldn't be impossible to find out the Windows IDs.)

like image 181
Jon Skeet Avatar answered Oct 29 '22 07:10

Jon Skeet


TimeZone class has a method IsDaylightSavingTime that take a date as parameter and return a boolean indicating if that date in in daylightsavingtime for that timezone.

like image 29
Andrea Parodi Avatar answered Oct 29 '22 06:10

Andrea Parodi