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