最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

timezone - Javascript display various time zones - Stack Overflow

programmeradmin2浏览0评论

The following script should be displaying the current local time based on the offset of -10 (Hawaii), but it's not working.

Can't figure out where I'm going wrong.

<h3>Current Time in Arizona is 
<script type="text/javascript">
<!--
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10)
    minutes = "0" + minutes

    var suffix = "AM";
    if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
    }
    if (hours == 0) {
    hours = 12;
    }

    document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")
//-->
</script>
</h3>

The following script should be displaying the current local time based on the offset of -10 (Hawaii), but it's not working.

Can't figure out where I'm going wrong.

<h3>Current Time in Arizona is 
<script type="text/javascript">
<!--
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10)
    minutes = "0" + minutes

    var suffix = "AM";
    if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
    }
    if (hours == 0) {
    hours = 12;
    }

    document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")
//-->
</script>
</h3>
Share Improve this question edited Sep 5, 2013 at 20:59 Millhorn asked Sep 5, 2013 at 20:54 MillhornMillhorn 3,1767 gold badges45 silver badges92 bronze badges 5
  • 1 Is this all the code? calcTime() doesn't seem to exist anywhere... – Jeff Rupert Commented Sep 5, 2013 at 20:57
  • 2 where are defined hours or d? – Krzysiek Commented Sep 5, 2013 at 20:57
  • You need to include the code for calcTime and the definitions of hours and d. – Geeky Guy Commented Sep 5, 2013 at 20:58
  • Clearly, I didn't need the downvote. I need help! Obviously, I'm missing something here. I think document.Write(CalcTime('Hawaii', '-10')); should be document.Write(currentTime('Hawaii', '-10')); – Millhorn Commented Sep 5, 2013 at 21:00
  • So, I've updated the script above. What I'm trying to do is account for a timezone variation and I can't seem to get it right. Here's a jsfiddle – Millhorn Commented Sep 5, 2013 at 21:03
Add a ment  | 

2 Answers 2

Reset to default 5

First of all, the code you've shown just returns the current local time. It doesn't even attempt to change it for a specific time zone.

Secondly, you need to read the timezone tag wiki. In particular, read the section titled "Time Zone != Offset".

Now it just so happens that Arizona and Hawaii don't currently use daylight saving time, so you could adjust by offset if those were your only two concerns. But I'm sure you are looking for a more general solution.

To do it properly, you will need a library that implements the IANA time zone database. I list several of them here. For example, here is an example of displaying the current time in Los Angeles using moment.js with the moment-timezone plugin:

moment().tz("America/Los_Angeles").format("h:mm a")

If you're just looking for a quick and easy way to put a clock on your web site for a particular time zone, then I remend using the free solution offered by timeanddate..

Write a function to move a Date by some offset in minutes/your choice

function offsetDate(offsetMinutes, d) {
    if (d) d = new Date(d);
    else d = new Date();
    if (offsetMinutes) d.setUTCMinutes(d.getUTCMinutes() + offsetMinutes);
    return d;
}
offsetDate(-10*60); // Thu Sep 05 2013 12:03:06 GMT+0100 (GMT Daylight Time)

Now use UTC functions to get the time

发布评论

评论列表(0)

  1. 暂无评论