我如何改变到台北的时间?

时间:2012-12-18 03:13:08

标签: javascript

  

可能重复:
  Convert date to another timezone in javascript

如何让这个节目得到台北的时间?他们的东西要修理吗?或者我需要为它添加一些代码吗?

var yudan = "";
var now = new Date();
var month = now.getMonth() + 1;
var date = now.getDate();
var year = now.getFullYear();
if (year < 2000) year = year + 1900;

document.write(year + "." + yudan + month + "." + date + ".");

document.write("<span id=\"yudan_clock\"><\/span>");
var now,hours,minutes,seconds,timeValue;
function yudan_time(){
now = new Date();
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
timeValue = (hours >= 12) ? " " : " ";
timeValue += ((hours > 12) ? hours - 0 : hours) + ":";
timeValue += ((minutes < 10) ? " 0" : " ") + minutes + ":";
timeValue += ((seconds < 10) ? " 0" : " ") + seconds + "";
document.getElementById("yudan_clock").innerHTML = timeValue;
setTimeout(yudan_time, 100);}
yudan_time();

1 个答案:

答案 0 :(得分:0)

如果问题是让时钟显示在台北的时间,请使用getTimezoneOffset()方法。这使您可以定义UTC / GMT与所需时区之间的时间偏差(在台北,它是UTC + 8)。然后,您可以使用一组UTC计时方法,例如now.getUTCMonth()代替now.getMonth()

这就是代码的外观:

var yudan = "";
var now = new Date();
var month = now.getUTCMonth() + 1;
var date = now.getUTCDate();
var year = now.getUTCFullYear();
if (year < 2000) year = year + 1900;
document.write(year + "." + yudan + month + "." + date + ".");

document.write("<span id=\"yudan_clock\"><\/span>");
var now,hours,minutes,seconds,timeValue;
function yudan_time(){
now = new Date();
hours = now.getUTCHours() + (now.getTimezoneOffset()/60);
minutes = now.getUTCMinutes();
seconds = now.getUTCSeconds();
timeValue = (hours >= 12) ? " " : " ";
timeValue += ((hours > 12) ? hours - 0 : hours) + ":";
timeValue += ((minutes < 10) ? " 0" : " ") + minutes + ":";
timeValue += ((seconds < 10) ? " 0" : " ") + seconds + "";
document.getElementById("yudan_clock").innerHTML = timeValue;
setTimeout(yudan_time, 100);}
yudan_time();

请注意,getTimezoneOffset()方法以分钟为单位返回值;对于台北来说,它会返回480,所以你需要除以60来获得时间。

希望这有帮助!