how to calculate age from date of birth in javascript?

how to calculate age from date of birth in javascript – 2 ways to calculate age in Javascript first is Calculate age from date of birth to current date and Calculate the age between the given two dates.

how to calculate age from date of birth in javascript

Question : What is the formula for calculating age with a variable date of birth? here you can Calculate Age JavaScript Code and Explanation. First I define a variable called DOB to store a persons Date of Birth.

How to calculate age in JavaScript with demo?

HTML Code
Calculate age from date of birth to current date

 

here simply you can easy to convert user input value into date object and get difference from current date;

 function fnCalculateAge(){

     var getDtLive = document.getElementById("txtDOB").value;  
	 console.log(getDtLive);
	 
	 var born_date = new Date(getDtLive);
	  console.log(" birthDate"+ born_date);
	 
	 var diff_dt=Date.now() - born_date.getTime(); 
	 	 
	 var  ageDate = new Date(diff_dt); 
	 var total_calc_member_age=   Math.abs(ageDate.getUTCFullYear() - 1970);
	 alert(total_calc_member_age);
}

Calculate the age between given two date.

HTML code

 
 

js code
here simply convert user input values into date object and get difference between two date.

 function allCalcAge(){
    var sdi = document.getElementById("st_dt").value;  
    var edi = document.getElementById("end_dt").value;  	 
	 
	var sd = new Date(sdi);
	var ed  = new Date(edi);
	 	 
	var diff_dt=ed - sd.getTime(); 
	 	 
	var  ageDate = new Date(diff_dt); 
	var total_calc_member_age=   Math.abs(ageDate.getUTCFullYear() - 1970);
	alert(total_calc_member_age);
}

Don’t Miss : How to calculate date of birth from age in javascript?

I hope you get an idea about how to calculate age from date of birth in javascript.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment