Reputation: 4292
How can i create a date object, then convert to another timzone from a datestring with a timezone and format as below
var date="20160317T073000";
var format = "YYYYMMDDTHHmmss";
var timezone ="America/New_York"
var newTimezone="Asia/Kolkata"
i want the date to convert to newTimezone, i tried with moment.js, but its converting to browser timezone
date=moment(date,format);
date.tz(timezone);
console.log(moment(date).format());
Upvotes: 0
Views: 181
Reputation: 2503
Moment.js seems to have that : http://momentjs.com/timezone/
Convert Dates Between Timezones
var newYork = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london = newYork.clone().tz("Europe/London");
newYork.format(); // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format(); // 2014-06-01T17:00:00+01:00
To convert your format, that should do it :
'20160317T073000'.replace(/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})/, '$1-$2-$3T$4:$5:$6')
Upvotes: 1