3 examples of 'calculate date of birth from age in javascript' in JavaScript

Every line of 'calculate date of birth from age in javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
52export function ageAsOf(birthDate, date) {
53 if (!birthDate || !date || !birthDate.isValid() || !date.isValid()) {
54 return null;
55 }
56
57 const onOrAfterBirthday = (
58 date.month() > birthDate.month() || (
59 date.month() === birthDate.month()
60 && date.date() >= birthDate.date()
61 )
62 );
63
64 return (date.year() - birthDate.year() - (onOrAfterBirthday ? 0 : 1));
65}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1function age(birthday) {
2 var ageDifMs = Date.now() - birthday.getTime();
3 var ageDate = new Date(ageDifMs);
4 return Math.abs(ageDate.getUTCFullYear() - 1970);
5}
5public getAge(): number {
6 return moment().diff(moment(this.value), "years");
7}

Related snippets