Convert UTC time to a definable local time

There are probably a million threads about time conversion, but I cannot find the answer to my problem. So I’m making it a million and one…

I have my date/time stamps stored in UTC (ie number of second since the Unix Epoch). How do I convert this to a definable local time in JavaScript? With definable I mean any local time, not necessarily the machine local time.

In PHP I would do it as follows:


$utc = time(); // current UTC
$timezone = "America/New_York";
date_default_timezone_set($timezone);
echo date("Y-m-d H:i:s", $utc);

The above would give the current date/time in New York. I would like to be able to do this in JavaScript for any time given in UTC.

Ultimately, you need a timezone database that lists the timezones, their gmt offset, and daylight savings boundaries and offsets(if any).

Then you can use the info to perform the date arithmetic.

I would imagine there’s some existing javascript code written to do this, which interacts with such a database.

great, thx for the link - I’m going to give this library a try.
so I can also conclude that this is not built-in JavaScript functionality? There is a getTimezoneOffset function, unfortunately no way to set the timezone…

You can’t change the timezone on your visitor’s computer as that would make all the times on their computer wrong. The timezone JavaScript can read is that set in their operating system and identifies the timezone that they live in.

If you want to change the time by a given timezone offset then update the minutes in the time by timezone*60 (that ensures that 1/2 hour timezones will work).