I write a function to reverse UTC time to local time
function utcToLocal(utc){
var t = new Date(Number(utc));
d = [t.getFullYear(), t.getMonth(), t.getDate()].join('/');
d += ' ' + t.toLocaleTimeString();
return d;
}
but i can’t confirm this code is right?
You should be able to convert the UTC timestamp into a local date and just subtract the local offset (which is in minutes), so:
Where I am, the offset is -600, so I need to subtract -36,000,000 ms from UTC time (which actually adds 36,000,000 ms).
Edit
I may have misunderstood the question.
The internal value of a javascript date instance is a UTC time clip in milliseconds. So if
utcis such a time (e.g. 2012-08-19T00:00:00Z is 1345334400000), then the OP will create a date instance based on that value andtoLocaleTimeStringwill show an implementation dependent string of the local time for the supplied UTC time.So if the local timezone offset is say -6hrs, then
alert(new Date(1345334400000)))show something like Sat Aug 18 2012 18:00:00 GMT-600.I was assuming that the OP wanted to set the local time to the same time as the UTC time, e.g. that 2012-08-19T00:00:00Z would become 2012-08-19T00:00:00 local.