Skip to content

Instantly share code, notes, and snippets.

@louismanson
Last active June 25, 2019 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louismanson/f7ead68f38f3eab2ac2d5e54b97b6f17 to your computer and use it in GitHub Desktop.
Save louismanson/f7ead68f38f3eab2ac2d5e54b97b6f17 to your computer and use it in GitHub Desktop.
ISO 8601 to seconds
let iso8601toSeg = (duaration) => {
let mDuration = [];
let hours = duaration.match(/\d{1,2}[H]/);
let minutes = duaration.match(/\d{1,2}[M]/);
let seconds = duaration.match(/\d{1,2}[S]/);
mDuration['hours'] = hours ? hours[0] : "0H";
mDuration['minutes'] = minutes ? minutes[0] : "0M";
mDuration['seconds'] = seconds ? seconds[0] : "0S";
let mHours = parseInt(mDuration['hours'].substr(0, mDuration['hours'].length-1));
let mMinutes = parseInt(mDuration['minutes'].substr(0,mDuration['minutes'].length-1));
let mSeconds = parseInt(mDuration['seconds'].substr(0, mDuration['seconds'].length-1));
return toltalSeconds = (mHours * 60 * 60) + (mMinutes * 60) + mSeconds;
}
console.log(iso8601toSeg("PT15M35S")); //Returns a value of 935
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment