获取特定时区的时间 [英] Get time of specific timezone
本文介绍了获取特定时区的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JavaScript 日期
类&尝试使用 getDate()
方法获取当前日期。但显然它是加载系统日期&时间。我正在运行来自印度的代码,但我希望得到日期&英国时间使用相同的方法。我该怎么做?
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 ?
推荐答案
如果您知道UTC偏移量,那么您可以使用以下函数传递它并获取时间:
If you know the UTC offset then you can pass it and get the time using the 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'));
取自:将当地时间转换为另一个
这篇关于获取特定时区的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文