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