Military time conversion functions
2021 May 1st
These are JavaScript functions I made for converting military-time to minutes, and vice versa.
1function militaryToMinutes(string) {2 let h = Number(string.match(/[^:]+/)); //match first 1 or 2 numbers3 let m = Number(string.match(/(?<=:)../)); //match last 2 numbers4 return h * 60 + m;5}67function minutesToMilitary(num) {8 let h = Math.floor(num / 60);9 let m = num % 60;10 if (m < 10) {11 //if a zero is needed for military time12 let f = "0";13 m = f + `${m}`;14 }15 return `${h}:${m}`;16}1718militaryToMinutes("10:00"); //6001920minutesToMilitary(600); //'10:00'