如何使用moment.js解析ISO 8601格式的持续时间? [英] How do I parse an ISO 8601 formatted duration using moment.js?

查看:390
本文介绍了如何使用moment.js解析ISO 8601格式的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ISO 8601格式的持续时间,例如:PT5M或PT120S。

I have an ISO 8601 formatted duration, for eg: PT5M or PT120S.

有什么方法可以使用moment.js解析这些并获取数量在持续时间内指定的分钟?

Is there any way I can parse these using moment.js and fetch the number of minutes specified in the duration?

谢谢!

PS:我看了解析ISO 8601持续时间
将ISO 8601时间格式转换为正常持续时间

但是很敏锐要知道这是否可以随时使用。

but was keen to know if this was do-able with moment.

推荐答案

时刻 解析ISO格式的持续时间,开箱即用 moment.duration 方法:

moment does parse ISO-formatted durations out of the box with the moment.duration method:

moment.duration('P1Y2M3DT4H5M6S')

正则表达式是gnarly ,但支持许多边缘情况并且非常彻底已测试

The regex is gnarly, but supports a number of edge cases and is pretty thoroughly tested.

这篇关于如何使用moment.js解析ISO 8601格式的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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