Created
February 16, 2022 09:04
-
-
Save itsdonnix/66b06f21350ef90ef7f1afee8a81d63f to your computer and use it in GitHub Desktop.
Calculate age
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 calculateAge(year, month, date) { | |
const bornDate = new Date(year, month - 1, date); | |
const nowDate = new Date(); | |
const bornYear = bornDate.getFullYear(); | |
const nowYear = nowDate.getFullYear(); | |
let age = nowYear - bornYear - 1; | |
if ( | |
nowDate.getMonth() >= bornDate.getMonth() && | |
nowDate.getDate() >= bornDate.getDate() | |
) { | |
age += 1; | |
} | |
return age; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment