Skip to content

Instantly share code, notes, and snippets.

@moghanbari
Last active July 9, 2022 03:27
Show Gist options
  • Save moghanbari/32db44f079af4f1fff25fd7c43bfd12e to your computer and use it in GitHub Desktop.
Save moghanbari/32db44f079af4f1fff25fd7c43bfd12e to your computer and use it in GitHub Desktop.
Messy code with comments
// 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