0

I use the following code to calculate the difference between two numbers.

function differenceCalculation(num1, num2) {
  if (num1 > num2) {
    return num1 - num2
  } else {
    return num2 - num1
  }
}

On document.ready

var difference = differenceCalculation(visitorTime, firstOption);
console.log({difference});

This gives for instance the following values in the console:

visitorTime = 1831
firstOption = 1900
difference  = 69

But since this should return the difference in time, I need it to do the calculation with 60 as the ceiling instead of the default 100.

A simple solution would be to minus 40 from the difference, but is this a good solution?

var difference = ( differenceCalculation(visitorTime, firstOption) - 40 );
purple11111
  • 709
  • 7
  • 20
  • Does this answer your question? [How to calculate number of days between two dates?](https://stackoverflow.com/questions/542938/how-to-calculate-number-of-days-between-two-dates) I mean, you could certainly find an answer here, because you're essentially calculating the difference between two dates – nicael Mar 31 '22 at 16:52
  • @nicael just glanced that over and sure there are multiple ways in those answers to get what I want, but I asked this question too soon as I figured out the solution already. But thank you for your contribution. – purple11111 Mar 31 '22 at 16:56

2 Answers2

0

It looks like you want to calculate in "military" time (like in "at nineteenhundred fifteen").

So, that involves converting both time indicators to hours and minutes, then doing the subtraction of each (with carry if needed), and then converting it back to military time:

const toHourMinute = military => [Math.floor(military / 100), military % 100];
const toMilitary = (hour, minute) => hour*100 + minute;

function timeDifference(a, b) {
    if (a > b) return timeDifference(b, a);
    let [h1, m1] = toHourMinute(a);
    let [h2, m2] = toHourMinute(b);
    if (m2 < m1) { // Deal with a carry over
        m2 += 60;
        h2--;
    }
    return toMilitary(h2 - h1, m2 - m1);
}

var visitorTime = 1831
var firstOption = 1900
var difference = timeDifference(visitorTime, firstOption);
console.log({difference});
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    I don't think that's what op means, I believe he wanted to calculate difference between two dates like 18:31 and 19:00 (that would be 29 minutes) – nicael Mar 31 '22 at 16:51
  • @trincot thank you very much for answering. Especially with a code example. But I am indeed looking for what nicael commented. – purple11111 Mar 31 '22 at 16:53
  • Ah, yes, I see what you mean. Updated my answer. – trincot Mar 31 '22 at 17:04
0

Just shortly after submitting and responding to the comments, I figured out a way that worked for me. Be aware though that the linked question has better answers! Which is the reason I voted to close this question.

Anyway, I made these changes to my code, and now I get the required outcome.

var difference = differenceCalculation(visitorTime, firstOption);
if ( difference >= 30 ) {
  var difference = ( differenceCalculation(visitorTime, firstOption) - 40 );
}
console.log({difference});

In the console:

visitorTime = 1831
firstOption = 1900
difference  = 29

And if the first 2 values where different:

visitorTime = 1901
firstOption = 2000
difference  = 59
purple11111
  • 709
  • 7
  • 20