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.
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.
Share edited Aug 23, 2013 at 0:27 phouse512 6804 gold badges15 silver badges27 bronze badges asked Aug 22, 2013 at 23:42 AlexAlex 12.2k13 gold badges33 silver badges41 bronze badges 10- 3 This 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 puters have their clocks set properly. – Pointy Commented Aug 22, 2013 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. – user1864610 Commented Aug 22, 2013 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 Commented Aug 22, 2013 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 Commented Aug 22, 2013 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 Commented Aug 22, 2013 at 23:49
2 Answers
Reset to default 7The 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 puter 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'));
When daylight saving is not being observed in New Zealand, NZST is GMT+12:00.
Daylight saving in NZ mences on the last Sunday in September, when 0200 bees 0300, and ends on the first Sunday in April, when 0300 bees 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.