1

For sure, there is a lot of questions about Date objects and timezones but many of them are about converting the current time to another timezone, and others are not very clear about what they want to do.

I want to display the day, hour, minute etc. in an arbitrary timezone, in an arbitrary day. For example, I would like a function f(t, s) that:

  • given the timestamp 1357041600 (which is 2013/1/1 12:00:00 UTC) and the string "America/Los Angeles", would satisfy the comparison below:

    f(1357041600, "America/Los Angeles") == "2013/01/01 04:00:00"
    
  • given the timestamp 1372680000 (2013/07/01 12:00:00 UTC), would satisfy the comparison below:

    f(1357041600, "America/Los Angeles") == "2013/07/01 05:00:00"
    
  • will always behave this way even if the timezone in the browser is, let us say "Europe/London" or "America/São Paulo".

  • will always behave this way even if the time in the browser is, let us say 2014/02/05 19:32, or 2002/08/04 07:12; and

  • as a final restriction, will not request anything from the server side (because I'm almost doing it myself :) )

Is it even possible?

Community
  • 1
  • 1
brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • Yes possible. Either find a library that already does it, good chance with something like [moments.js](http://momentjs.com/) or if doing it yourself, grab the [tz database](http://en.wikipedia.org/wiki/Tz_database) (a.k.a. Olson database) convert the data to a javascript object and use it as a lookup for your function. – Xotic750 Feb 05 '14 at 23:20
  • possible duplicate of [How to initialize javascript date to a particular timezone](http://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone) – Matt Johnson-Pint Feb 06 '14 at 05:54
  • See specifically my answer in that dup post for a list of libraries that solve this exact problem. – Matt Johnson-Pint Feb 06 '14 at 05:55

2 Answers2

2

For JavaScript runtime environments that support the ECMAScript Internationalization API, and adhere to its recommendation of supporting the IANA time zone database, you can simply do this:

new Date(1357041600000).toLocaleString("en-US", {timeZone: "America/Los_Angeles"})

For other environments, a library is required. There are several listed here.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

given the timestamp 1357041600 (which is 2013/1/1 12:00:00 UTC)

That appears to be seconds since the UNIX epoch (1970-01-01T00:00:00Z). Javascript uses the same epoch, but in milliseconds so to create a suitable date object:

var d = new Date(timestamp * 1000);

That will create a Date object with a suitable time value. You then need to determine the time zone offset using something like the IANA time zone database. That can be applied to the Date object using UTC methods. E.g. resolve the offset to minutes, then use:

d.setUTCMinutes(d.getUTCMinutes() + offset)

UTC methods can then be used to get the adjusted date and time values to create a string in whatever format you require:

var dateString = d.getUTCFullYear() + '/' + pad(d.getUTCMonth() + 1) + '/' ...

where pad is a function to add a leading zero to single digit values. Using UTC methods avoids any impact of local time zone offsets and daylight saving variances.

There are also libraries like timezone.js that can be used to determine the offset, however I have not used them so no endorsement is implied.

RobG
  • 142,382
  • 31
  • 172
  • 209