简体   繁体   English

在Moment.js Node.js中将毫秒转换为日期

[英]Convert milliseconds to date in moment.js nodejs

i want to know how can i convert milliseconds to date format in moment.js using nodejs. 我想知道如何使用nodejs在moment.js中将毫秒转换为日期格式。

I am using the following way:- 我正在使用以下方式:-

var schedule_time="1512986378692";  // Monday, 11 December 2017 15:29:38

schedule_time time is 11 dec, 2017 15:29:38. schedule_time时间是2017年12月11日15:29:38。

But when i use the following method it give me the following result 但是当我使用以下方法时,它给我以下结果

moment.unix((schedule_time)/1000).format("YYYY-MM-DD HH:mm:ss"); // 2017-12-11 09:59:38

This is because of the timezone difference. 这是因为时区不同。 The milliseconds are of IST, but moment is by default using UTC for this on your machine. 毫秒是IST,但是默认情况下是使用UTC在您的计算机上使用矩。

You can try a good library which integrates well with MomentJS, and have good features for working with timezones. 您可以尝试一个很好的库 ,该可以与MomentJS 很好地集成,并且具有使用时区的良好功能。

To answer the quest, moment-timezone offers an API to change the timezone of the provided moment object, like this: 为了回答这个问题,moment-timezone提供了一个API来更改所提供的moment对象的时区,如下所示:

moment(1512986378692).tz('Asia/Kolkata').format('YYYY-MM-DD HH:mm:ss')

Also, I didn't quite understand why are you converting the millis to seconds further. 另外,我不太了解为什么将毫秒转换为秒。

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

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