使用moment.js格式化小时,分钟和秒 [英] Format hours, minutes and seconds with moment.js

查看:4399
本文介绍了使用moment.js格式化小时,分钟和秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从后端服务171054获得此值.它代表hh:mm:ss.但是,当我使用文档中的格式设置选项时,它会带回我00:00:00.

I get this value from my backend service: 171054. It represents hh:mm:ss. But when I use the formatting options from the docs it gives me back 00:00:00.

我尝试过的事情:

moment('171054').format('hh-mm-ss')
moment('171054').format('HH-mm-ss')
moment('171054').format('HH-MM-SS')

推荐答案

您在 format 选项和解析选项.由于您输入的字符串不是ISO 8601格式,因此您必须在解析时指定格式.

You are confusing format option with parsing option. Since your input string is not in ISO 8601 format, you have to specify format when parsing.

以下是您的用例的可行示例:

Here a working example for your use case:

var mom = moment('171054', 'HHmmss');
console.log(mom.format());
console.log(mom.format('HH:mm:ss'));

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>

这篇关于使用moment.js格式化小时,分钟和秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆