Skip to content

Instantly share code, notes, and snippets.

@guilsa
Created January 24, 2021 17:42
Show Gist options
  • Save guilsa/bc3a7117adc9ce7026307cc0474c3d74 to your computer and use it in GitHub Desktop.
Save guilsa/bc3a7117adc9ce7026307cc0474c3d74 to your computer and use it in GitHub Desktop.
countingMins.js
function CountingMinutes(str) {
const convertTo24hours = (time) => {
let [ hours, mins ] = time.split(':')
if (mins.includes('pm') && hours >= 1 && hours <= 11) {
hours = parseInt(hours) + 12
}
if (mins.includes('am') && hours == 12) {
hours = '00'
}
return `${hours}:${mins.substring(0, mins.length - 2)}`
}
const minsSinceMidnight = (t) => {
let [ hours, mins ] = t.split(':')
return (parseInt(hours) * 60) + parseInt(mins)
}
const getHours = (t) => parseInt(t.split(':')[0])
const getMins = (t) => parseInt(t.split(':')[1])
let [ t1, t2 ] = str.split('-')
t1 = convertTo24hours(t1)
t2 = convertTo24hours(t2)
let t1MinsSinceMidnight = minsSinceMidnight(t1)
let t2MinsSinceMidnight = minsSinceMidnight(t2)
if (t2MinsSinceMidnight < t1MinsSinceMidnight) {
return minsSinceMidnight(t2) - (((getHours(t1) - 24) * 60) + getMins(t1))
} else {
return t2MinsSinceMidnight - t1MinsSinceMidnight
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment