I know how to use new Date(UTCStrings) to local timezone.
But now, the question is how to convert an UTCString to other timezone (not local).
e.g.
The UTCString is ‘1338480000000’.
My local timezone is UTC+4.
I want to convert the date(UTCString) to UTC+9.
How can I do it?
Appreciate for your help!
Update
Thanks for BalaKrishnan’s help.
I follow BalaKrishnan’s key points maked a simple function. Hopefully, this will help others.
function utcToOtherTimezone(utcString, timezone){
var isoDt = new Date(utcString), // do this to convert it to iso time:
dt = isoDt.addMinutes( isoDt.getTimezoneOffset() + (timezone * 60) );
return dt.toLocaleDateString() + ' ' + dt.toLocaleTimeString();
}
$('#dtime').html(utcToOtherTimezone(1341282169000, +8));
And don’t forget to add datejs
Online testing example http://jsfiddle.net/ysjia/FWbZ8/. Enjoy it.
First create a Date object from the UTCString as follows:
var utcString = 1338480000000;
// This will however be in local time, not iso time.
var isoDt = new Date(utcString);
// do this to convert it to iso time:
isoDt.addMinutes( isoDt.getTimezoneOffset()
);
// addMinutes is an API from Date.js.
Now the isoDt object has it’s date value the same as the UTC date, to which you can add the necessary offset +9 or whatever.
Refer to this jquery faq that discusses this:
http://jqfaq.com/how-to-parse-a-date-string-disregarding-time-zones/