[英]Convert UTC Date and Time to local Date and Time using Moment.js
In my mongo date is getting stored as 2020-10-01T05:32:41.379Z
.在我的 mongo 日期被存储为
2020-10-01T05:32:41.379Z
。 I am trying to convert to my local time which is 5:30 hrs after the UTC time.我正在尝试转换为我的本地时间,即 UTC 时间后 5:30。 I tried using
moment(new Date(update.planned_date)).zone("+0530").toDate())
.我尝试使用
moment(new Date(update.planned_date)).zone("+0530").toDate())
。 But i am still getting the same time 2020-10-01T05:32:41.379Z
.但我仍然得到相同的时间
2020-10-01T05:32:41.379Z
。 My expected output is 2020-10-01T11:02:41.379Z
which is 5:30 after the UTC time.我的预期输出是
2020-10-01T11:02:41.379Z
,即 UTC 时间后 5:30。
Thanks谢谢
Try local()
from momentjs to get the local time equivalent of the UTC从 momentjs 尝试
local()
以获取等效于 UTC 的本地时间
const date = moment .utc("2020-10-01T05:32:41.379Z", "YYYY-MM-DD hh:mm:ss") .local() .format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"); console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.