Convert Date() to a Different Time Zone Using JavaScript

Scenario

User would like to get the current date in a different time zone using JavaScript.

Solution

In the following code snippet, currDate variable holds the current date and time in the given time zone:

var toTimeZone=”+5:30″;
var tz;
if(toTimeZone.search(“:”)==-1){
tz=parseFloat(toTimeZone);
}else{
tz=parseFloat(toTimeZone.slice(0,toTimeZone.search(“:”)))+parseFloat(toTimeZone.slice(toTimeZone.search(“:”)+1)/60)
}
var date = new Date();
var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
var currDate = new Date(utc + (3600000*tz));
currDate = currDate.toString();
var currDate = currDate.slice(0, currDate.search(‘ GMT’))+” GMT”+(toTimeZone.search(“\+”)==-1?”+”:””)+toTimeZone;

Leave a comment

Your email address will not be published. Required fields are marked *