Facebook returns this date
2010-12-16T14:39:30+0000
However, I noticed that it's 5 hours ahead of my local time. It should be:
2010开发者_如何转开发-12-16T09:39:30+0000
How can I convert this to local time in javascript?
Edit
After seeing some responses, I feel I should define what I'm searching for more clearly. How would I be able to determine the user's local time zone to format the date?
Here is the function to parse ISO8601 dates in Javascript, it also handles time offset correctly: http://delete.me.uk/2005/03/iso8601.html
This might help you:
taken from 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'));
Here's how I did it in Javascript
function timeStuff(time) {
var date = new Date(time);
date.setHours(date.getHours() - (date1.getTimezoneOffset()/60)); //for the timezone diff
return date;
}
精彩评论