Reputation: 45
Api call returns the below JSON object:
{
"status": "OK",
"distance": "482 km",
"duration": "8 hours 46 mins"
}
Now I need this "duration" to be converted and sent as timestamp in this format - https://en.wikipedia.org/wiki/ISO_8601#Durations How to go about it?
Upvotes: 0
Views: 637
Reputation: 5931
You can use Moment.js which has support to convert the date to ISO 8601 string.
All you have to do is extract hours and minutes from the string.
let d = '8 hours 46 mins'.split(' ')
let result = moment.duration(`${d[0]}:${d[2]}`, 'H:M').toISOString()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
Upvotes: 1