This article describes the method of JS calculating age based on birthday. Share it for your reference. The specific implementation method is as follows:
function parseDate(str){ if((/^\d{4}[\-\/\s+]\d{1,2}[\-\/\s+]\d{1,2}$/)){ return new Date((/[\-\/\s+]/i,'/')); } else if((/^\d{8}$/)){ return new Date((0,4)+'/'+(4,6)+'/'+(6)); } else{ return ('An error occurred in the time conversion! '); } } function GetAgeByBrithday(birthday){ var age=-1; var today=new Date(); var todayYear=(); var todayMonth=()+1; var todayDay=(); var birthday=parseDate(birthday); if(parseDate(birthday)!='An error occurred in the time conversion! ') { birthdayYear=(); birthdayMonth=(); birthdayDay=(); if(todayYear-birthdayYear<0) { alert("Birth date selection is wrong!"); } else { if(todayMonth*1-birthdayMonth*1<0) { age = (todayYear*1-birthdayYear*1)-1; } else { if(todayDay-birthdayDay>=0) {//alert(thisDay+'-'+brithd+"_ddd"); age = (todayYear*1-birthdayYear*1); } else { age = (todayYear*1-birthdayYear*1)-1; } } } return age*1; } else { return -1; } }
I hope this article will be helpful to everyone's JavaScript programming.