admin 管理员组文章数量: 1086019
I have an iso-8601 duration which is as follows:
PT15M51S
I want to convert the duration into seconds so that I can store it in the db and order by duration etc.
Here is what I tried so far:
var moment = require('moment');
var duration = 'PT15M51S';
var x = moment(duration, moment.ISO_8601);
console.log(x.seconds());
Which results in NaN
.
I have an iso-8601 duration which is as follows:
PT15M51S
I want to convert the duration into seconds so that I can store it in the db and order by duration etc.
Here is what I tried so far:
var moment = require('moment');
var duration = 'PT15M51S';
var x = moment(duration, moment.ISO_8601);
console.log(x.seconds());
Which results in NaN
.
- 1 momentjs is huge. If you are only converting the value, use this instead: npmjs./package/iso8601-duration – Jonathan Commented Mar 23, 2018 at 18:19
1 Answer
Reset to default 8You need to use moment.duration
function. Also, to get the total value in seconds, use asSeconds()
instead of seconds()
.
In your case:
var moment = require('moment');
var duration = 'PT15M51S';
var x = moment.duration(duration, moment.ISO_8601);
console.log(x.asSeconds()); // => 951
本文标签: javascriptHow to convert iso8601 duration to seconds using momentStack Overflow
版权声明:本文标题:javascript - How to convert iso-8601 duration to seconds using moment? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743988597a2514307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论