Created
June 11, 2018 12:03
-
-
Save sagar-gavhane/287608203e396d6aa33a5a003215c2b7 to your computer and use it in GitHub Desktop.
function convert to seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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