Skip to content

Instantly share code, notes, and snippets.

@Sauloxd
Last active March 30, 2019 11:15
Show Gist options
  • Save Sauloxd/c82b9de5a6a944ad6bd8396b7e341171 to your computer and use it in GitHub Desktop.
Save Sauloxd/c82b9de5a6a944ad6bd8396b7e341171 to your computer and use it in GitHub Desktop.
How old are you?
const getDayMonthYear = (date) => ({
day: date.getDate(),
month: date.getMonth(),
year: date.getFullYear()
});
const pastBirthday = (birthday, today) => (today.month >= birthday.month) && (today.day >= birthday.day)
const age = (birthdayString) => {
const today = getDayMonthYear(new Date());
const birthday = getDayMonthYear(new Date(birthdayString));
return today.year - birthday.year + (pastBirthday(birthday, today) ? 0 : -1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment