Skip to content

Instantly share code, notes, and snippets.

@sagar-gavhane
Created June 11, 2018 12:03
Show Gist options
  • Save sagar-gavhane/287608203e396d6aa33a5a003215c2b7 to your computer and use it in GitHub Desktop.
Save sagar-gavhane/287608203e396d6aa33a5a003215c2b7 to your computer and use it in GitHub Desktop.
function convert to seconds
const convertToSeconds = (time) => {
if (!Array.isArray(time.split(':'))) {
return false;
}
let result =
time.split(':').length > 2 &&
time.split(':').map((t, i) => {
t = parseInt(t);
if (i === 0) {
return t * 60 * 60;
}
if (i === 1) {
return t * 60;
}
return t;
});
if (Array.isArray(result)) {
return result.reduce((acc, val) => acc + val, 0);
}
return false;
};
const sec = convertToSeconds('12:0:0');
const mint = sec/60;
const hours = sec/3600;
console.log(`sec: ${sec}`);
console.log(`mint: ${mint}`);
console.log(`hours: ${hours}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment