How to calculate time between two different times?

Challenge : https://www.codewars.com/kata/simple-time-difference/train/javascript

I’m just wondering if anyone knows how to calculate time in JS like for eg. between “18:05” and “06:24” it is 11 hours and 41 minutes.

You can subtract one Date from another to get the difference in milliseconds. Then you do math to get hours, minutes, etc.

im trying to subtract “18:05” and “06:24” but their in string format. using number() or parseint/parsefloat doesn’t work. how can i convert them into num form?

If you’re given two strings, you’ll need to parse it into numbers yourself.

i can’t seem to parse the ones after the colon. u know how?

You may want to look at .split().

Have you gone through the FCC Basic JavaScript curriculum. A lot of what you are asking is covered in that section. Splitting strings separated by a specific character is a common task.

ye i completed the js certificate. sometimes i just forget. just tryna polish my skills even more by banging out algorithms

I can’t seem to convert the strings into numbers… do you advice replacing the colons with periods to convert them?

function solve(arr){

 for ( let i = 0; i < arr.length; i++) { 
   let splitNum = arr[i].split("");
   for ( let j = 0; j < splitNum.length; j++) {
  splitNum[j] =  Number(splitNum[j])
   }
 }
 console.log(arr[0])
 
 console.log(typeof arr[0] === "number")
}

solve(["23:00","04:22","18:05","06:24"])

You should look more carefully at how to use split().

can’t figure it out man any other tips? do i need to put something in split other than just 2 quotes

What exactly does this do?

arr[i].split("");

it splits them up like that so i can convert every single string though it don’t work image

What would splitString look like after the following is executed? If you are not sure, you need to go back and read how to use the split method.

var string = "36|68";
var splitString = string.split("|");

3|6|||6|8 it splits every character with whats inside the argument

You should test the code and check for yourself.

anybody willing to help?

Alright I gave it a try. I got the sample tests right but when I tried to do an attempt I don’t know what’s happening because it expects some strange values that don’t seem correct. Or maybe I don’t understand the problem hmmm…

i been here 9 hours and these mods don’t bother helping

Instead of splitting the array and to convert each one, you can just simplify the process by replacing the semi colon with a dot, with what you suggested earlier. And then just loop over the array converting the new strings into numbers, doing so will take away so many unnecessary steps for conversion. And then after you get the final calculations with the max interval, replace the dot with the semi colon again.