Last active
September 26, 2021 12:45
-
-
Save pedroglcbarros/b1e9c160447f81874719c49d97338783 to your computer and use it in GitHub Desktop.
Function to countdown the time between two dates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code isn't 100% complete, I am still working on it.