0

im trying to get date object with current time of specific time zone,

    var options = {
        timeZone: 'Asia/Novokuznetsk',
        year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false
    },
    formatter = new Intl.DateTimeFormat([], options),
    date_string = formatter.format(new Date()),      
    dsplit = date_string.split(","),
    date_arr = dsplit[0].split("/"),
    time_arr = dsplit[1].split(":"),
    now = new Date(date_arr[2], date_arr[0] - 1, date_arr[1], time_arr[0], time_arr[1], time_arr[2]);

And now outputs correct current time of specific timezone. Main problem is that not all timezones are supported by this example. Is there any better way to get date object of current time in specific timezone?

1 Answers1

0

Adomas: Try the following code in order to specify any timezone on the calcTime function:

<html>
<head>
<script language="JavaScript">

// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset) {

    // create Date object for current location
    d = new Date();

    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();

}

// get Bombay time
alert(calcTime('Bombay', '+5.5'));

// get Singapore time
alert(calcTime('Singapore', '+8'));

// get London time
alert(calcTime('London', '+1'));

</script>
</head>
<body>

</body>
</html>

Reference: http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/

Joel Hernandez
  • 2,195
  • 1
  • 13
  • 17
  • I guess your suggestion should fit, but i dont know offset. Timezone format Asia/Novokuznetsk, Eupore/Kiev etc. – Adomas Kondrotas Aug 09 '16 at 14:34
  • You can get the actual offset with this: var offset = new Date().getTimezoneOffset(); console.log(offset); – Joel Hernandez Aug 09 '16 at 14:40
  • And it would be offset of my time zone. How to know offset of Europe/Kiev time zone if i we dont know current time in Kiev? – Adomas Kondrotas Aug 09 '16 at 14:43
  • 1
    Adomas, Javascript it's not so powerful when it comes to timezones. If you could use something like PHP, you could retrieve any timezone, using an specific city: http://php.net/manual/en/timezones.php. What you can do it's try to replicate this in Javascript, creating an array with all the available timezones. – Joel Hernandez Aug 09 '16 at 14:50
  • 1
    Well with this one i must use ony client-side technologies.. Dont have access to server side, but i found this: http://momentjs.com/timezone/docs/ might be the right thing to use. Thanks to all comments – Adomas Kondrotas Aug 09 '16 at 14:54
  • Please read "Time Zone != Offset" in [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Aug 09 '16 at 19:57