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().

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

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

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.

yeah that’s what i suggested earlier but the mods are so vague in what their saying, im left with the impression that what im doing is wrong so i go many other countless routes for the past 9 hours. anyways, now converting strings to number format isn’t the problem. now i’m just so confused with the algorithm that i could only pass 1 test case out of 202. my code :

function solve(arr){

let msChecker = 10000000000000000;
for ( let i = 0; i < arr.length; i++) {   
  if (i % 2 === 0) { 
    console.log(arr[i])
let end = new Date("August 21, 2019 " + arr[i]);
let start = new Date("August 21, 2019 " + arr[i+1]);
let ms = Math.floor(end - start);
console.log(ms)
if ( ms < msChecker) { 
  msChecker = ms
}
  }
}
 let minutes = Math.floor(msChecker/1000/60)
let hours =  Math.floor(msChecker/1000/60/60)
console.log(hours)
console.log(minutes)
minutes = minutes - (hours*60)
console.log(minutes)
minutes-=1;
return hours.toString().concat(":",minutes.toString())
}

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

// ,"11:40")


// In this Kata, you will be given a series of times at which an alarm goes off. Your task will be to determine the maximum time interval between alarms. Each alarm starts ringing at the beginning of the corresponding minute and rings for exactly one minute. The times in the array are not in chronological order. Ignore duplicate times, if any.

// let sorted = arr.sort((a,b)=> a>b?1:-1)
// console.log(sorted)

// let msCheck = 0;
// for ( let i = 0; i < sorted.length; i++) { 
// let start = new Date("August 21, 2019 " + sorted[i]);

// let j = i+1
// while (j < sorted.length) { 
// let end = new Date("August 21, 2019 " + sorted[j])
// let ms = Math.floor(end - start);
// console.log(ms)
// if (ms > msCheck ) { 
//   msCheck = ms
// }
// j++
// }

// }

// let startt = new Date("August 21, 2019 04:22:00");
// let endd = new Date("August 21, 2019 23:00:00")
// console.log(endd-startt)

don’t take the entirety of my code literally. i did many tweaks to the point im so desperate to even pass 1 test case. if not i wouldn’t even be able to pass a single one.

I took a look at the Kata, and can guide you in the way that I would do it. It seems that you are trying to incorporate Date(), which you wouldn’t need to do.

So first, you know that the array can contain only one number. You can do a simple if statement for that to clear that case, and the great part about this, is that you don’t need to do any calculations, because it it’s only one number, the max interval will always be 23:59. So all you would need to do is just return “23:59”.

For cases that it’s more than one number, you are on the right track with the sort, as you would need to sort them from highest to lowest, of course after converting them into numbers. After you have those sorted numbers, you would need to subtract the two adjacent numbers, while doing so you need to take into account the 1 minute interval that the kata says that the alarm will go off on.

Given that, you would subtract .01 from the result that you get. But theres another catch, if the numbers after the decimal is over 60, that obviously wouldn’t’ be correct, as there’s only 60 minutes in an hour, so to account for that as well, you can do an if statement to check if those preceding numbers after the decimal point is over 60, and if so subtract 40 from it.

After all of that is said and done, you would just return the highest number in that new calculated array, of course converting the dot back to a semicolon. So now it’s just up to you to write that code out with the above logic.