Skip to content

Instantly share code, notes, and snippets.

@iboB
Last active February 20, 2021 08:48
Show Gist options
  • Save iboB/1363623 to your computer and use it in GitHub Desktop.
Save iboB/1363623 to your computer and use it in GitHub Desktop.
Simple age calculator in JavaScript (I use it in some about-me HTMLs)
function calcAge(year, month, day) {
const now = new Date();
let age = now.getFullYear() - year;
const mdif = now.getMonth() - month + 1; // jan is 0 in getMonth
if (mdif < 0) {
// not birthday yet
--age;
}
else if (mdif === 0) {
// maybe birthday?
var ddif = now.getDate() - day;
if (ddif < 0) --age; // not birthday yet
}
return age;
}
@bennami
Copy link

bennami commented Dec 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment