简体   繁体   中英

Javascript date convert to timezone with format

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());

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM