简体   繁体   中英

Format date in JS using Moment.js

There is the following code:

console.log(order.start_time);
console.log(moment(order.start_time).format("HH:MM"));

I just want to get hour and minute from date using moment and display it. Output:

2014-06-30T09:00:00.000Z
13:06

But I don't understand why my date is formatted incorrectly. Thanks in advance.

Please note that there is a difference between MM and mm . The first formatting option is used for months , while the second is for days .

More information here: http://momentjs.com/docs/#/parsing/string-format/

From the doc here: http://momentjs.com/docs/#/displaying/format/

You should use HH:mm. Then your time will have right format.

Further information, the time might be incorrectly as you expected because timezone. Momentjs auto display time in your system timezone.

Update: To display time in specified Timezone, use "zone" method:

console.log(moment(order.start_time).zone('+00').format("HH:mm"));

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