Last active
July 9, 2022 03:27
-
-
Save moghanbari/32db44f079af4f1fff25fd7c43bfd12e to your computer and use it in GitHub Desktop.
Messy code with comments
This file contains 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
// Returns number of remaining days to birthday | |
function birthday(inputDate) { | |
const today = new Date(); | |
const birthday = new Date(inputDate); | |
const upcomingBday = new Date(today.getFullYear(), birthday.getMonth(), birthday.getDate()); | |
// if birthday has already passed, set next year as birthday | |
if (today > upcomingBday) { | |
upcomingBday.setFullYear(today.getFullYear() + 1); | |
} | |
// getTime() returns in milliseconds so we divide the result in milliseconds to get number of days | |
const result = Math.ceil((upcomingBday.getTime() - today.getTime()) / (24 * 60 * 60 * 1000)); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment