首页 > 解决方案 > momentjs 不会按照指定的时区更改日期/时间

问题描述

我正在使用 moment 和 moment-timezone 来输出存在日期(对于任何时区)。对于我的时区(亚洲/雅加达),今天 2020-04-22 17:30:00 是正确的。但是,它总是为任何时区输出 2020-04-22 17:30:00,这是不正确的。在下面的示例中,它应该显示 2020-04-22 15:30:00

const presence_end = "2020-04-22T17:30:00+07:00";
const end = Moment(presence_end).tz('Asia/Jayapura').format();
console.log(end) //the output is 2020-04-22 17:30:00```

标签: javascriptnode.jsmomentjs

解决方案


I can't quite replicate your results, however I hope this is helpful.

From the code below, the time seems to be changing correctly, from Asia/Jakarta time (+07:00) to Asia/Jayapura time (+9:00).

If it is 17:30 hours in Jakarta, it should be 19:30 hours in Jayapura.

const presence_end = "2020-04-22T17:30:00+07:00";
const endJakarta = moment(presence_end).tz('Asia/Jakarta').format();
console.log("Presence_end (Asia/Jakarta):", endJakarta )
console.log("Presence_end (UTC):", moment(presence_end).toISOString());
const endJayapura = moment(presence_end).tz('Asia/Jayapura').format();
console.log("Presence_end (Asia/Jayapura):", endJayapura);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> 
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script> 


推荐阅读