discord_bot/util/timer/timeUntil.js
SileNce5k 8c642f16f0
All checks were successful
CI / CI (push) Successful in 1m23s
Rename argument name to something more sensible
and add comment explaining off by 1 error
2025-05-14 07:52:19 +02:00

20 lines
905 B
JavaScript

module.exports = function(targetTime) {
let countDownDate = new Date(targetTime).getTime();
let now = new Date().getTime();
let distance = countDownDate - now;
let days = Math.floor(distance / (1000 * 60 * 60 * 24));
let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (seconds < 0) { // Due to how the math above works, if the input time is in the past, the time will be off by 1.
days = days + 1;
hours = hours + 1;
minutes = minutes + 1;
seconds = seconds + 1;
}
const totalInSeconds = (days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds;
return { days: days, hours: hours, minutes: minutes, seconds: seconds, totalInSeconds: totalInSeconds };
}