3

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>
Millhorn
  • 2,953
  • 7
  • 39
  • 77
  • 1
    Is this all the code? `calcTime()` doesn't seem to exist anywhere... – Jeff Rupert Sep 05 '13 at 20:57
  • 2
    where are defined `hours` or `d`? – Krzysiek Sep 05 '13 at 20:57
  • You need to include the code for `calcTime` and the definitions of `hours` and `d`. – Geeky Guy Sep 05 '13 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 Sep 05 '13 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](http://jsfiddle.net/8ymRb/) – Millhorn Sep 05 '13 at 21:03

2 Answers2

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 recommend using the free solution offered by timeanddate.com.

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

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

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • Do you mean like this? `Date.UTC(year, month, day[, hours[, minutes[, seconds[,ms]]]])` The time is the only thing that will display. But, it needs to be able to display the time based on the clicked state. So, clicking on Texas will show one time, and California will show another time. Not sure how to "use UTC functions"... – Millhorn Sep 05 '13 at 21:34
  • @webfrogs `.getUTCMinutes()`, `.getUTCHours()`, etc – Paul S. Sep 05 '13 at 22:44
  • Basically what I'm saying is, rather than trying to move timezone, adjust the _Date_ so the UTC values are what you'd expect the local time to be in that timezone, then read out the _UTC_ time. – Paul S. Sep 05 '13 at 22:46