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?
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?
Share Improve this question edited Dec 16, 2010 at 15:07 asked Dec 16, 2010 at 14:57 user356808user356808 1- The '+0000' is the time zone. If you're on PST that part would be '-0500'. – Rup Commented Dec 16, 2010 at 14:59
3 Answers
Reset to default 4Here 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;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742324602a4422493.html
评论列表(0条)