Using Javascript to calculate age
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 - 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](http://shots.codepen.io/tipsoftheday/pen/hwFde-512.jpg)
Developer | Victor Yan |
Username | tipsoftheday |
Uploaded | January 05, 2023 |
Rating | 3 |
Size | 2,538 Kb |
Views | 16,192 |
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!
Name | Size |
A Pen by Victor Yan | 2,757 Kb |
Tooltip using plain Javascript | 1,825 Kb |
Testing for a Value in JavaScript Array | 1,914 Kb |
Detect Enter Key Pressed Using jQuery event | 1,822 Kb |
TodoMVC using jQuery | 6,388 Kb |
HTML 5 Local Storage Example | 1,912 Kb |
Tooltip using jQuery UI | 2,124 Kb |
Tooltip using Bootstrap | 2,452 Kb |
Bootstrap 3 button groups | 1,510 Kb |
Navigation menu using Angular Directive | 2,391 Kb |
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!
Name | Username | Size |
Progressively reveal dots on a Bezier curve | GreenSock | 2,489 Kb |
Elephants Full screen site | Orrinward | 3,981 Kb |
Break Out | AzazelN28 | 12,431 Kb |
Sidebar Thing | Jonambas | 2,779 Kb |
Elon Musk - Tribute Page - FreeCodeCamp | Yunnimun | 8,615 Kb |
Box-sizing | Elad2412 | 1,572 Kb |
Flat design iframe | Damienpm | 1,819 Kb |
Lazy Load for Background Images | The_ruther4d | 2,977 Kb |
Jstam.com Home Page | Jstam | 10,558 Kb |
Perspective Origin Demo | Agelber | 3,614 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!