Facebook返回此日期
2010-12-16T14:39:30+0000
然而,我注意到它比我当地时间提前了5个小时。它应该是:
2010-12-16T09:39:30+0000
如何在javascript中将其转换为本地时间?
修改
在看到一些回复之后,我觉得我应该更清楚地定义我正在寻找的内容。我如何能够确定用户的当地时区来格式化日期?
答案 0 :(得分:4)
这可能会对您有所帮助:
取自Convert the local time to another time zone with this JavaScript
// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset) {
// create Date object for current location
d = new Date();
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));
// return time as a string
return "The local time in " + city + " is " + nd.toLocaleString();
}
// get Bombay time
alert(calcTime('Bombay', '+5.5'));
// get Singapore time
alert(calcTime('Singapore', '+8'));
// get London time
alert(calcTime('London', '+1'));
答案 1 :(得分:4)
这是在Javascript中解析ISO8601日期的函数,它还正确处理时间偏移: http://delete.me.uk/2005/03/iso8601.html
答案 2 :(得分:0)
以下是我在Javascript中的表现方式
function timeStuff(time) {
var date = new Date(time);
date.setHours(date.getHours() - (date1.getTimezoneOffset()/60)); //for the timezone diff
return date;
}