简体   繁体   English

时间戳转换为日期格式给我错误的日期在 JavaScript

[英]Timestamp conversion in to Date format giving me Wrong date In JavaScript

In my application, A user from US created a offer on 30th November 2020, while getting the search response in UI, I am getting created timestamp as 1606786157.343,When I am converting in to date format its returning me 1st Dec 2020 even in UTC format.在我的应用程序中,来自美国的用户于 2020 年 11 月 30 日创建了一个报价,在 UI 中获得搜索响应时,我创建的时间戳为 1606786157.343,当我转换为日期格式时,即使是 UTC 格式,它也会在 2020 年 12 月 1 日返回我.

I tried this solution - moment.unix(1606786157.343).utc().format("MM/DD/YYYY")我尝试了这个解决方案 - moment.unix(1606786157.343).utc().format("MM/DD/YYYY")

How should I convert so that I can get exact 30th November 2020 in Indian or US timezone?我应该如何转换才能在印度或美国时区获得准确的 2020 年 11 月 30 日?

Please help请帮忙

We can use the moment.unix() constructor to accept the number of seconds since the Unix Epoch.我们可以使用 moment.unix() 构造函数来接受自 Unix Epoch 以来的秒数。 Then we can use moment-timezone to convert to other timezones:然后我们可以使用 moment-timezone 转换为其他时区:

 const timeUtc = moment.unix(1606786157.343).utc(); console.log("US Time (New York):", timeUtc.tz("America/New_York").format("MM/DD/YYYY HH:mm:ss")); console.log("US Time (LA):", timeUtc.tz("America/Los_Angeles").format("MM/DD/YYYY HH:mm:ss")); console.log("US Time (Chicago):", timeUtc.tz("America/Chicago").format("MM/DD/YYYY HH:mm:ss")); console.log("India Time:", timeUtc.tz("Asia/Kolkata").format("MM/DD/YYYY HH:mm:ss"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data-10-year-range.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM