Clock in different time zones

*爱你&永不变心* 提交于 2019-12-01 04:47:34

You can add or subtract hours from your date with

currentTime.setHours(currentTime.getHours()+offset);

where offset is any number of hours.

I updated the jsfiddle with that line and the function so that it accepts a parameter for the offset. This is not the UTC offset, just how many hours to add from the system time. You can get the current UTC offset by currentTime.getTimezoneOffset()/60

Demo

currentTime.getTimezoneOffset() will give you the time difference between Greenwich Mean Time (GMT) and local time, in minutes.

You can use the value to calculate time in required timezone.

Matt Johnson-Pint

To do this properly, you will need a time zone database, such as one of the ones I listed here.

All of the other answers to this question are making the mistake in thinking that a "time zone" and a "time zone offset" are the same thing. They are not. For example, the time zone for London is Europe/London, which can have either a +0 or +1 offset depending on what time of year it is. The time zone for New York is America/New_York, which can have either a -5 or -4 offset based on a completely different set of dates than London.

You might want to look at moment-timezone:

moment().tz("America/New_York").format()

Check out http://www.datejs.com/ it handles dates and timezones nicely

if you check out the demo, in the text box, put 1 GMT vs 1 MST or 1 EST and it will pop out a nice date for you wrt/ that time zone

Thank you everyone for your help. It was all getting a bit confusing but I found a good example here which is how I ended up working it out. Check out example 4: http://www.ajaxupdates.com/jclock-jquery-clock-plugin/

Hope it's useful for someone else!

I use on m php website the following:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"
<script type="text/javascript">
$(function() {
    getStatus();
});

function getStatus() {
<?php
echo "    var z = '".strftime("%Z",strtotime("now"))."';\n";
?>
    var d = new Date();
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    var offset = -8;
    if(z == "PDT") offset = -7;
    var currentTime = new Date(utc + (3600000*offset));
    var currentHours = currentTime.getHours ( );
    var currentMinutes = currentTime.getMinutes ( );
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;
    var currentTimeString = currentHours + ":" + currentMinutes + ": " + timeOfDay + " " + z;

    $('td#clock').html(currentTimeString);
    setTimeout("getStatus()",5000);
}
</script>

Where i use a table, so this will fill

<table><tr><td id='clock'></td></tr></table>

with the time from Los Angeles (PDT or PST) however my server also runs on that time so passing the strftime from your server might not produce the same effect. But as stated this was my resolve to get a working time in a other zone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!