0

I need a way to convert times in the future to different timezones without relying on the user's computer time.

At registration time, the user supplies his timezone. When he logs in, I calculate the offset in minutes between the UTC time and his time and inject that offset into the page so that a javascript function can do the conversions. Something like this:

var TheUTCTime = new Date(UserTime.getTime() - TimeZoneOffsetInMinutes * 60000);

and like this for the other way around:

var TheUserTime = new Date(UTCTime.getTime() + TimeZoneOffsetInMinutes * 60000);

This works really well to convert times as long as the offset doesn't change. For instance, because of daylight saving, between US EST and UTC, there's a difference of 300 minutes or 360 minutes depending on the month in the year.

My functions work well to convert today's date but I'd like something that can 1) do the same thing for any day of the year and 2) doesn't depend on the user's internal clock or timezone.

How could I do this?

Thanks.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • Why don't you want to depend on the browser's timezone? What if the user is travelling? By the way, look into `Date#getUTC*` – Dagg Nabbit Mar 02 '12 at 20:02

2 Answers2

1

My functions work well to convert today's date but I'd like something that can 1) do the same thing for any day of the year and 2) doesn't depend on the user's internal clock or timezone.

If you want to convert another UTC time into the user's local time, you have to know their time zone. That's pretty much the definition of a time zone: a mapping between UTC and local time (or equivalently, between UTC and the offset from local time).

As you've seen, getting the current offset isn't enough, because of daylight saving transitions (and any other changes to time zones - they vary more than you might expect).

Basically there's no way round this: you will have to ask the user for their time zone. You can make a good guess based on the current offset from UTC and possibly geocoding their IP address, but you'll have to confirm it with them. (For example, they may be on a trip or something, and not in their "home" time zone.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Well yes, I know the user's timezone: he supplies it at registration and I use it to calculate his offset of the day. For the moment, I'm injecting in the page that offset but I could also inject his timezoneID but how do I convert .net's timezoneIDs into javascript IDs that I can use. – frenchie Mar 02 '12 at 20:13
  • @frenchie: It's not really clear what you're trying to do - I would personally keep all the time zone conversions on the server, to avoid worrying about what time zones the browser knows about. When you need to display a local time, get the server to translate it into the user's time zone. – Jon Skeet Mar 02 '12 at 20:19
  • ok, I was sending times to the page only in UTC and then converting them to local. I'm changing things around to send times either directly in local time if there's need to compute anything on the page or with both local and UTC if there's something to compute on the page with the date sent. That way, there's both no need to convert times for display purposes and the UTC time is there for compute when needed. – frenchie Mar 02 '12 at 22:41
0

To don't depend on the user's clock timezone, I think the best approach is to do conversions in the server side.

There's a good question here in the SO that covers daylight saving time and javascript Date object: Daylight saving time and time zone best practices

Community
  • 1
  • 1