JSFiddle - React, Tailwind, and code Playground

by Jessie Lau

HTML

<!-- - Make a new function taking a string parameter
- Split string into the time (spliting on dash char)
- call new function (f2) which returns how many minutes have passed since midnight for both string
- return difference between this two returned values


f2("9:30am"):
-extract the two last chars of the string in a new var
-split the time string on ":" (arr= ["9","30"])
- if "am" -> arr[0]* 60 + ar[1]
- else "pm" -> (arr[0]+12) *60 + arr[1]
- return 
 -->

JavaScript

function CountingMinutesI(str){
    var separate_two_times = str.split("-");
    var totalMinutes = totalMin(separate_two_times[1]) - totalMin(separate_two_times[0]);
    return totalMinutes;    
}

function totalMin(time){ // "7:30am"
    var am_pm = time.slice(-2); // 'am'
    var hour_min = time.match(/\d+/g); //["7","30"]
    if(am_pm == "pm"){
        hour_min[0] += 12;
    }
    var res = hour_min[0]*60 + hour_min[1];
    return res;
}
console.log(CountingMinutesI("7:00am-9:00am"));