简体   繁体   English

使用Moment.js在JS中格式化日期

[英]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 . 请注意, MMmm之间存在差异。 The first formatting option is used for months , while the second is for days . 第一个格式化选项使用几个月 ,而第二格式化则使用days

More information here: http://momentjs.com/docs/#/parsing/string-format/ 此处的更多信息: http : //momentjs.com/docs/#/parsing/string-format/

From the doc here: http://momentjs.com/docs/#/displaying/format/ 从此处的文档中: http : //momentjs.com/docs/#/displaying/format/

You should use HH:mm. 您应该使用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. Momentjs在您的系统时区中自动显示时间。

Update: To display time in specified Timezone, use "zone" method: 更新:要显示指定时区中的时间,请使用“区域”方法:

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

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

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