javascript - How to convert iso-8601 duration to seconds using moment? - Stack Overflow

I have an iso-8601 duration which is as follows:PT15M51SI want to convert the duration into seconds so

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.

Share Improve this question asked Apr 26, 2015 at 14:11 Angular noobAngular noob 4375 silver badges11 bronze badges 1
  • 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
Add a ment  | 

1 Answer 1

Reset to default 8

You 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743988596a4539367.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信