Allow dates to be used in timer command
All checks were successful
CI / CI (push) Successful in 1m24s
All checks were successful
CI / CI (push) Successful in 1m24s
And some general cleanup of a couple of functions.
This commit is contained in:
parent
c74f323c1b
commit
ed77c2aaf1
5 changed files with 48 additions and 27 deletions
20
util/timer/timeUntil.js
Normal file
20
util/timer/timeUntil.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
module.exports = function(targetTime) {
|
||||
const countDownDate = new Date(targetTime).getTime();
|
||||
const now = new Date().getTime();
|
||||
|
||||
const 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, hours, minutes, seconds, totalInSeconds };
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue