I want to show the current time in New Zealand using JQuery. My client has customers calling from outside New Zealand. However, he gets annoyed that customers call at 3 am because they did not think of the time difference. He wants to add a label saying "Current time in New Zealand is: 3.00 am". Can it be achieved via JQuery? Cheers.
-
3This is not a jQuery thing at all. You can use plain JavaScript to get the time from the client machine, but you would be safer using your own server's time. You can't be sure people's computers have their clocks set properly. – Pointy Aug 22 '13 at 23:45
-
jQuery doesn't know anything about the time, so no, it can't be done with jQuery. However, jQuery can update the page with the time in NZ, so yes it can be done by jQuery. – Aug 22 '13 at 23:45
-
Let's say my PC battery (yep, the little one) is dead, on every PC start my current time will be `00:00` and I'll call your client! :D – Roko C. Buljan Aug 22 '13 at 23:46
-
@MikeW that's a red herring. The correct answer is: "Display the time from the server, don't use javascript at all". – Hamish Aug 22 '13 at 23:48
-
@Hamish what if my server is in Germany and you call me from Australia? I'll be pissed any way :D – Roko C. Buljan Aug 22 '13 at 23:49
-
Javascript to convert from local time to another timezone: http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/ – Barbara Laird Aug 22 '13 at 23:50
-
@RokoC.Buljan What? I didn't say "display the server time", I said, "Display the time from the server". Ie, render the current NZ time with the server, don't try to guess it from the browser locale. – Hamish Aug 22 '13 at 23:50
-
@Hamish That's _an_ answer (anda reasonable one), but rarely in computing is there _one_ right answer. – Aug 22 '13 at 23:51
-
(javascript date/time handling is notoriously confusing) – Hamish Aug 22 '13 at 23:51
-
@Hamish http://stackoverflow.com/questions/863474/automatically-detect-users-current-local-time-with-javascript-or-php – Roko C. Buljan Aug 22 '13 at 23:55
2 Answers
The JavaScript Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.
function DisplayCityTime(city, offset) {
// Date object for current location
var aDate = new Date();
// UTC time in msec
var utc = adate.getTime() + (adate.getTimezoneOffset() * 60000);
// Date object for the requested city
var newdate = new Date(utc + (3600000*offset));
// return time as a string
return "The local time for city : "+ city +" is "+ newdate.toLocaleString();
}
alert(DisplayCityTime('Montreal', '-5'));

- 115
- 6
-
-
-
3change your computer's time and tell me what you see. A wrong date for sure AFAIK – Roko C. Buljan Aug 23 '13 at 00:12
When daylight saving is not being observed in New Zealand, NZST is GMT+12:00.
Daylight saving in NZ commences on the last Sunday in September, when 0200 becomes 0300, and ends on the first Sunday in April, when 0300 becomes 0200. NZDT is GMT+1300
There are a number of time services that you can query to get the current time in any time zone, try answers to this question: Web service - current time zone for a city?.
So the server, wherever it is in the world, no matter how it's configured, simply gets the current time in NZ as required and sends it to the client. You can probably do it from the client too, but the server will be more reliable.
It's trivial to do a time conversion in javascript, however adjustments for daylight saving and reliance on local client settings mean it's not particularly robust or reliable.