Skip to content

Instantly share code, notes, and snippets.

@AlexanderArmua
Last active May 15, 2022 21:50
Show Gist options
  • Save AlexanderArmua/3377250f587371c34015bc481352f3e3 to your computer and use it in GitHub Desktop.
Save AlexanderArmua/3377250f587371c34015bc481352f3e3 to your computer and use it in GitHub Desktop.
function dateToSeconds(date) {
const dateTime = new Date(date);
return dateTime.getTime() / 1000;
}
function getDateMocked() {
const date = new Date();
date.setHours(17);
date.setMinutes(0);
date.setSeconds(0);
return date;
}
function secondsToString(seconds) {
const secondsInOneDay = 86400;
const secondsInOneHour = 3600;
const secondsInOneMinute = 60;
const days = Math.floor(seconds / secondsInOneDay);
const hours = Math.floor((seconds % secondsInOneDay) / secondsInOneHour);
const minutes = Math.floor(((seconds % secondsInOneDay) % secondsInOneHour) / secondsInOneMinute);
const secondsLeft = Math.floor(((seconds % secondsInOneDay) % secondsInOneHour) % secondsInOneMinute);
return `${days}:${hours}:${minutes}:${secondsLeft}`;
}
const currentTimeInSeconds = dateToSeconds(new Date());
const serverTimeInSeconds = dateToSeconds(getDateMocked()); // TODO: Change this to the server time or something else.
const distanceBetweenTimes = serverTimeInSeconds - currentTimeInSeconds;
function getTimeLeft() {
return secondsToString(distanceBetweenTimes);
}
console.log(`${distanceBetweenTimes} seconds left.`);
console.log(getTimeLeft());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment