Using Javascript to calculate age

Developer
Size
2,538 Kb
Views
16,192

How do I make an using javascript to calculate age?

Using Javascript to calculate age based on the date of birth and the date to calculate. What is a using javascript to calculate age? How do you make a using javascript to calculate age? This script and codes were developed by Victor Yan on 05 January 2023, Thursday.

Using Javascript to calculate age Previews

Using Javascript to calculate age - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Using Javascript to calculate age</title> <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class='indent-20'> <h2>Using Javascript to calculate age</h2> <p> Your date of birth: <input id="dob"> </p> <p> Your age: <span id="age"></span> </p> <div style="margin-top: 20px;">Sponsored by <a onclick="window.open('https://www.solargalore.com.au')">Solar Galore</a></div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Using Javascript to calculate age - Script Codes CSS Codes

 .indent-20{	margin: 10px; }

Using Javascript to calculate age - Script Codes JS Codes

$(document).ready(function () { handleDOBChanged();
});
//listener on date of birth field
function handleDOBChanged() { $('#dob').on('change', function () { if (isDate($('#dob').val())) { var age = calculateAge(parseDate($('#dob').val()), new Date());	$("#age").text(age); } else { $("#age").text(''); } });
}
//convert the date string in the format of dd/mm/yyyy into a JS date object
function parseDate(dateStr) { var dateParts = dateStr.split("/"); return new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
}
//is valid date format
function calculateAge (dateOfBirth, dateToCalculate) { var calculateYear = dateToCalculate.getFullYear(); var calculateMonth = dateToCalculate.getMonth(); var calculateDay = dateToCalculate.getDate(); var birthYear = dateOfBirth.getFullYear(); var birthMonth = dateOfBirth.getMonth(); var birthDay = dateOfBirth.getDate(); var age = calculateYear - birthYear; var ageMonth = calculateMonth - birthMonth; var ageDay = calculateDay - birthDay; if (ageMonth < 0 || (ageMonth == 0 && ageDay < 0)) { age = parseInt(age) - 1; } return age;
}
function isDate(txtDate) { var currVal = txtDate; if (currVal == '') return true; //Declare Regex var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; var dtArray = currVal.match(rxDatePattern); // is format OK? if (dtArray == null) return false; //Checks for dd/mm/yyyy format. var dtDay = dtArray[1]; var dtMonth = dtArray[3]; var dtYear = dtArray[5]; if (dtMonth < 1 || dtMonth > 12) return false; else if (dtDay < 1 || dtDay > 31) return false; else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31) return false; else if (dtMonth == 2) { var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0)); if (dtDay > 29 || (dtDay == 29 && !isleap)) return false; } return true;
}
Using Javascript to calculate age - Script Codes
Using Javascript to calculate age - Script Codes
Home Page Home
Developer Victor Yan
Username tipsoftheday
Uploaded January 05, 2023
Rating 3
Size 2,538 Kb
Views 16,192
Do you need developer help for Using Javascript to calculate age?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Victor Yan (tipsoftheday) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!