javascript - 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```
解决方案
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>
推荐阅读
- java - Maven 资源路径
- reactjs - 使用带有 grahql 的 AWS 放大创建新用户时出现“未经授权”错误
- mapbox - 某些地区的 MapBox 数据质量问题
- angular - NGRX 使用 CombineLatest 防止无限循环
- javascript - 如何在没有任何新依赖项的情况下在 React JS 中使用此 CSS 代码?
- angular - 角度将属性总和绑定到属性
- amazon-web-services - 更改 docker 映像时更新 Sagemaker 端点
- c# - SignalR,如何验证连接 ID 是否仍处于活动状态?
- javascript - javascript:未捕获的类型错误:无法读取未定义的属性“X”
- c# - C# WPF 材料设计 MaterialDesignFlatButton 提示\高光颜色