Skip to content

Instantly share code, notes, and snippets.

@pedroglcbarros
Last active September 26, 2021 12:45
Show Gist options
  • Select an option

  • Save pedroglcbarros/b1e9c160447f81874719c49d97338783 to your computer and use it in GitHub Desktop.

Select an option

Save pedroglcbarros/b1e9c160447f81874719c49d97338783 to your computer and use it in GitHub Desktop.
Function to countdown the time between two dates.
function countdown(targetDate) {
const now = new Date();
const future = new Date(targetDate)
const feb29 = new Date(`February 29 ${future.getFullYear()} 00:00:00`);
const isLeapYear = feb29.getDate() === 29 ? true : false;
const years = future.getFullYear() - now.getFullYear();
const months = (future.getMonth() + 1) - (now.getMonth() + 1) >= 0 ?
(future.getMonth()) - (now.getMonth()) :
(11 - now.getMonth()) + future.getMonth();
let days;
switch (future.getMonth() + 1) {
case 1: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (29 - now.getDate()) + future.getDate();
break;
case 2: days = future.getDate() - now.getDate() < 0 && !isLeapYear ? (26 - now.getDate()) + future.getDate() : (future.getDate() - 1) - (now.getDate());
break;
case 2: days = future.getDate() - now.getDate() < 0 && isLeapYear ? (27 - now.getDate()) + future.getDate() : (future.getDate() - 1) - now.getDate();
break;
case 3: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (29 - now.getDate()) + future.getDate();
break;
case 4: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (28 - now.getDate()) + future.getDate();
break;
case 5: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (29 - now.getDate()) + future.getDate();
break;
case 6: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (28 - now.getDate()) + future.getDate();
break;
case 7: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (29 - now.getDate()) + future.getDate();
break;
case 8: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (29 - now.getDate()) + future.getDate();
break;
case 9: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (28 - now.getDate()) + future.getDate();
break;
case 10: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (29 - now.getDate()) + future.getDate();
break;
case 11: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (28 - now.getDate()) + future.getDate();
break;
case 12: days = future.getDate() - now.getDate() > 0 ? (future.getDate() - 1) - now.getDate() : (29 - now.getDate()) + future.getDate();
break;
default: future.getDate() - now.getDate();
break
};
const hours = future.getHours() - now.getHours() > 0 ?
(future.getHours() + 1) - (now.getHours() + 1) :
(23 - now.getHours()) + future.getHours();
const minutes = future.getMinutes() - now.getMinutes() > 0 ?
(future.getMinutes() + 1) - (now.getMinutes() + 1) :
(59 - now.getMinutes()) + future.getMinutes();
const seconds = future.getSeconds() - now.getSeconds() > 0 ?
(future.getSeconds() + 1) - (now.getSeconds() + 1) :
(59 - now.getSeconds()) + future.getSeconds();
return console.log(`${years} years, ${months} months, ${days} days, ${hours} hours, ${minutes} minutes, ${seconds} seconds`);
}
const date = () => console.log(countdown(/* ENTER DATE IN FORMAT: MONTH DATE YEAR HOUR:MINUTE:SECOND*/));
setInterval(date, 1000);
@pedroglcbarros
Copy link
Author

pedroglcbarros commented Sep 17, 2021

This code isn't 100% complete, I am still working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment