[英]Moment js format duration
I got an ISO 8601 string as duration and I need to format it as XhYm( 1h20m). 我有一个ISO 8601字符串作为持续时间,我需要格式化为XhYm(1h20m)。 Does anyone have some suggestions?
有人有什么建议吗?
What I did right now is this: 我现在做的是:
const duration = moment.duration(secondData.duration);
const formatted = moment.utc(duration.asMilliseconds()).format('HH:mm');
To get the output format you want, you'll need to set up the format string differently in the format()
call: 要获得所需的输出格式,您需要在
format()
调用中以不同方式设置格式字符串:
const duration = moment.duration('PT1H20M');
const formatted = moment.utc(duration.asMilliseconds()).format("H[h]m[m]");
Using the square brackets makes moment print those characters without trying to use them in the format. 使用方括号可以立即打印这些字符,而无需在格式中使用它们。 See the Escaping Characters in the momentjs documentation.
请参阅momentjs文档中的转义字符 。
The simplest way to do it involves a little bit of manual formatting: 最简单的方法是进行一些手动格式化:
var d = moment.duration("PT1H20M"); console.log(d.hours()+"H"+d.minutes()+"M");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.