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

javascript - get current time in a specific country via jquery - Stack Overflow

programmeradmin1浏览0评论

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
 |  Show 5 more ments

2 Answers 2

Reset to default 7

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 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.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>