Last active
July 3, 2021 15:52
-
-
Save iamwill123/e13130a7348887dc12a927765de59ae8 to your computer and use it in GitHub Desktop.
converting 12 to 24 hours
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 isAm = (s = '', regex = /am/) => regex.test(s.toLowerCase()) | |
const getHead = (s = '') => s.split(':')[0] | |
const getTail = (s = '') => s.slice(2, 8) | |
const convertToNum = (s = '') => parseInt(s) | |
const convertedNum = (s = '') => convertToNum(getHead(s)) | |
const calcAm = (s = '') => | |
convertedNum(s) === 12 | |
? (convertedNum(s) - 12).toString() + 0 | |
: convertedNum(s) < 10 | |
? '0' + convertedNum(s) | |
: convertedNum(s) | |
const calcPm = (s = '') => | |
convertedNum(s) === 12 ? 24 - convertedNum(s) : convertedNum(s) + 12 | |
const calcAmOrPm = (s = '') => (isAm(s) ? calcAm(s) : calcPm(s)) | |
function timeConversion(s) { | |
const tail = getTail(s) | |
const time = calcAmOrPm(s) + tail | |
return time | |
} | |
const s = '01:00:00PM' | |
timeConversion(s) // 13:00:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment