如何使用javascript获取具体时区的时间? [英] How to get Time of specific timezone using javascript?
本文介绍了如何使用javascript获取具体时区的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
如果你知道UTC偏移量,那么你可以传递它,并使用以下函数获取时间:
函数calcTime(city,offset){
//为当前位置创建Date对象
var d = new Date();
//转换为msec
//减去本地时区偏移
//获取UTC时间(以msec为单位)
var utc = d.getTime() - (d .getTimezoneOffset()* 60000);
//为不同的城市创建新的Date对象
//使用提供的偏移
var nd = new Date(utc +(3600000 * offset));
//返回时间作为字符串
返回城市的当地时间+城市+是+ nd.toLocaleString();
}
alert(calcTime('Bombay','+5.5'));
I am using a javascript Date class & trying to get the current date using getDate() method. But obviously it is loading system date & time. I am running the code from India but I want to get the date & time of UK using the same method. How can I do that ?
解决方案
If you know the UTC offset then you can pass it and get the time using following function:
function calcTime(city, offset) {
// create Date object for current location
var d = new Date();
// convert to msec
// subtract local time zone offset
// get UTC time in msec
var utc = d.getTime() - (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
var nd = new Date(utc + (3600000*offset));
// return time as a string
return "The local time for city"+ city +" is "+ nd.toLocaleString();
}
alert(calcTime('Bombay', '+5.5'));
这篇关于如何使用javascript获取具体时区的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文