how to code a check age function in javascript

Solutions on MaxInterview for how to code a check age function in javascript by the best coders in the world

showing results for - "how to code a check age function in javascript"
Antonio
08 Mar 2016
1function Person(dob) {
2  // [1] new Date(dateString)
3  this.birthday = new Date(dob); // transform birthday in date-object
4  
5  this.calculateAge = function() {
6    // diff = now (in ms) - birthday (in ms)
7    // diff = age in ms
8    const diff = Date.now() - this.birthday.getTime(); 
9    
10    // [2] new Date(value); -> value = ms since 1970
11    // = do as if person was born in 1970
12    // this works cause we are interested in age, not a year
13    const ageDate = new Date(diff); 
14    
15    // check: 1989 = 1970 + 19
16    console.log(ageDate.getUTCFullYear()); // 1989
17    
18    // age = year if person was born in 1970 (= 1989) - 1970 = 19
19    return Math.abs(ageDate.getUTCFullYear() - 1970);
20  };
21}
22var age =new Person('2000-1-1').calculateAge();
23console.log(age); // 19
24
queries leading to this page
check age jscalculating age using javascriptcalculate age in javascriptjavascript get age from dateconvert age to days from birthday javascriptget exact age from data jsjs function to calculate agecalculate age from birthdate javascripthow to calculate age from date of birth in javascriptjavascript code to calculate age in yearshow to find someone 27s date of birth from date in javascriptnode js age countingtime and date stamp to calculate aging javascriptcalculate age javascriptjavascript funtion to calculate age above 18calculate age from dob in javascriptjavacript age from datefunction to check age in javascript calculate age jshow to check age in javascriptuse javascript to get ageconvert year into age jqueryis there a function in javascript to calculate age in datehow to calc age jsvar age 3d 7bperson 7dhow to calculate age from date of birth jsget age from date javascriptjavascript get birthdate with minimum age check age number in javascript function htmlcalculating age in javascript through date functionjs calculater ageformat age in age group in javascriptcalculate age using form jshow to find age today in javascriptjavascript calculate date of birth how to calculate a person 27s age with javascriptget current age by birthday jsget date of birth from user html jsget date of birth in age javascriptcalculate age from dob html javascriptcalculate age at certain year in javascriptjs get age from datejs birthday to agejavascript calculate age todays datecreate a javascript function with the name age 28 29 this function will take your date of birth as parameters and return your age in years check age in javascriptjs calculate age functiondetermine someons exact age with date object jsage from date function javascriptage calculation in jsjavascript get the age from datejs date to agejavascript work out age from date of birthage javascriptjavascript calculate ageclacualting age in jscalculate dob from age in javascriptage of java scriptjs how to calculate your ageage find with date of birth jsjavascript function take birthdate and return age calculate age by birth date javascriptyear calculator for age code in javascriptstore birth year in variable javascriptcompare value to the date 28 29 to obtain age javascriptget age from date in javascriptbirthday to age javascripthow to calculate age in days in javascript using returncalculate age from date of birth in node jswrite a function in js that calculates the born dayjavascript function to calculate age from date of birthjavascript calculate age using arrow functioncalc age in jshow to calculate birth year from age in javascripthow to code a check age function in javascriptjavascript html get agehow to check age jshow to calculate age from date of birth javascriptcalculate age acurate javascriptget age in javascriptjavascript age from datejavascript get birthdayget your age using dates jscalculate age from dob jsjavascript age from birthdateget year with a birth date to methods javascriptcalculate current age java scriptage function in javascriptcheck specific age based on day jsjavascript agecheck age javascripthow to calculate your birthday using javascriptage mwthod jscalculate age from year of birth in javascriptcalculate age from date of birth javascripthow to calculate age from date parse valuesget age according two date jsjavascript calculate afejavascript code to calculate age from date of birthjs input date of birth calcjavascript age difference validationcalculate age from date of birth in javascriptnode js calculate ageage calculate using jsjavascript calculate birth datehow to determine the age and days in javascript to a given date of birthmethod to calculate age from date of birth in javascripthow to calculate age in jsmethod to compute age from birth year jsjavascript to calculate user 27s age 2c based on dob in javascriptjavascript calculate born datehow to find age javascriptjavascript age functionnode js age countingcalculate age using date of birth javascriptget a person 27s age with two javascript datesjs dob to yearsdob jsjavascript get agehow to code a check age function in javascript