简体   繁体   中英

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.

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.

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.

You can try a good library which integrates well with MomentJS, and have good features for working with timezones.

To answer the quest, moment-timezone offers an API to change the timezone of the provided moment object, like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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