简体   繁体   English

使用 Moment.js 将 UTC 日期和时间转换为本地日期和时间

[英]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.

相关问题 Moment.js:将UTC日期从数据库转换为本地时间 - Moment.js: Convert UTC date from database into local time 在javascript(moment.js)中将UTC日期转换为当地时间 - convert UTC date to local time in javascript (moment.js) 我需要使用 moment.js 根据本地时区转换 UTC 日期和时间,如何将日期从 UTC 转换为 html 中的时区 - I need to convert UTC date and time based on the local timezone using moment.js, how to convert date from UTC to timezone in html 如何使用moment.js和moment-timezone.js将特定时区(非本地)的给定日期/时间转换为UTC - How to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js 使用 moment.js 将日期转换为 UTC - Convert date to UTC using moment.js moment.js无法将utc日期转换为本地日期 - moment.js unable to convert utc date to local date 使用 moment.js 将这些 GMT 时间/日期字符串转换为 UTC 时间/日期 - Convert these strings of GMT time/date to UTC time/date with moment.js Moment.js 将本地时间转换为 UTC 时间确实有效 - Moment.js Convert Local time to UTC time does work 使用moment.js处理本地化日期以将其转换为本地日期/时间 - Processing localised date to convert it to local date/time using moment.js 我如何在 javascript 中使用 moment.js/Date 将 EST 时间数据转换为本地时间 - how I convert EST time data to local time using moment.js / Date in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM