1

Possible Duplicate:
Convert date to another timezone in javascript

How to make this program get Taipei's time? Is their something to fix? or do I need to add some code for it?

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();
Community
  • 1
  • 1
  • You can change that `year` thing into just `var year = now.getFullYear();`, by the way. And the `setTimeout` should be more like `setTimeout(yudan_time, 100);`. Or just a `setInterval` with a delay of `1000`. – Ry- Dec 18 '12 at 03:15

1 Answers1

0

If the issue is getting the clock to show the time at Taipei, use the getTimezoneOffset() method. This lets you define how the offset in time between the UTC/GMT and the desired timezone (in Taipei, it's UTC+8). Then, you can use the set of UTC timing methods, like now.getUTCMonth() in place of now.getMonth().

So this is how your code might look:

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();

Keep in mind, the getTimezoneOffset() method returns a value in minutes; for Taipei, it would return 480, so you need to divide by 60 to get the hours.

Hope this helps!

Shrey Gupta
  • 5,509
  • 8
  • 45
  • 71