如何在moment.js中格式化日期时保留时区?

时间:2015-10-22 14:59:42

标签: javascript momentjs

我有2015-10-24T17:12-05:00之类的日期,而且我使用moment.js将其格式化为:

moment('2015-10-24T17:12-05:00').format('h:mm A');

而不是在字符串中指定的时区中显示时间,moment.js似乎将其转换为我的计算机的时区。我在格式化时如何保留时区?

1 个答案:

答案 0 :(得分:0)

var estTime = '2019-09-01T14:30:00-04:00';
var istTime = "2019-10-01T23:20:00+05:30";
console.log(estTime , "  ", moment(estTime).format("hh:mm") ," ", moment.parseZone(estTime).format("hh:mm"));
console.log(istTime, "  ", moment(istTime).format("hh:mm"), " ", moment.parseZone(istTime).format("hh:mm"));

Moment的字符串解析函数,例如moment(string)moment.utc(string)接受偏移信息(如果提供),但是将所得的Moment对象转换为本地或UTC时间。相反,moment.parseZone()解析字符串,但将得到的Moment对象保留在字符串中提供的偏移量为固定偏移量的时区中。

parseZone

输出:

  

2019-09-01T14:30:00-04:00 02:30 02:30

     

2019-10-01T23:20:00 + 05:30 01:50 11:20