JavaScript Age Calculator – Solution: Age Calculator using JavaScript with source code, demo, and also how you can execute full program easily. To complete the program you need to follow those steps given bellow
Live Demo
Also Helpful –
Age Calculator using JavaScript
Description
Back to coding, To complete this program with debugging free code you have to create three files. The first file is to HTML and the second file gonna be CSS for design part and the third file JavaScript for function-related,
index.html
Create an HTML file named ‘index.html‘ and put those codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<!-- Coding Debugging (https://codingdebugging.com) --> <!DOCTYPE html> <html> <head> <title>Age Calculator - Coding Debugging</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <!-- Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <!-- Google Fonts --> <link href="https://fonts.googleapis.com/css2?family=Baloo+Thambi+2:wght@400;500;600;700;800&display=swap" rel="stylesheet"> <!-- My Stylesheet --> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container-fluid bg-light p-5 text-center my-3"> <h2 class="">Age Calculator</h1> <h5 class="text-muted">CALCULATE YOUR AGE WITH YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS & MILISECONDS.</h5> </div> <div class="container bg-light p-3 my-5"> <div class="card-body"> <div class="row"> <div class="col-md-12"> <button class="btn btn-dark">Date of Birth</button> </div> </div> <br> <div class="row mb-3"> <div class="col-md-6"> <input type="date" class="form-control" id="birth_date" name="birth_date"> </div> </div> <div class="row text-right"> <div class="col-md-12"> <input type="button" class="btn btn-primary " value="Calculate Age" onclick="ageCalculate()"> </div> </div> </div> <div class="card-footer"> <div class="row" align="left"> <div class="col-md-12"> <button class="btn btn-dark">Result</button> </div> </div> <br> <div id="age"></div> </div> </div> <hr> <footer class="footer mt-auto py-3 bg-light"> <div class="container text-center"> <span class="text-muted">Copyright ©2020 | Age Calculator | <a href="https://codingdebugging.com/" target="_blank">CodingDebugging.Com</a></span> </div> </footer> <!-- JavaScript Page --> <script src="function.js" type="text/javascript"></script> </body> </html> |
style.css
Create a CSS file named ‘style.css‘ and put those codes given below.
1 2 3 |
body { font-family: "Baloo Thambi 2", cursive; } |
function.js
Create a JavaScript file named ‘function.js‘ and put those codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
function ageCalculate(){ var birthDate =document.getElementById('birth_date').value; var d = new Date(birthDate); var mdate = birthDate.toString(); var yearThen = parseInt(mdate.substring(0,4), 10); var monthThen = parseInt(mdate.substring(5,7), 10); var dayThen = parseInt(mdate.substring(8,10), 10); var today = new Date(); var birthday = new Date(yearThen, monthThen-1, dayThen); var differenceInMilisecond = today.valueOf() - birthday.valueOf(); var year_age = Math.floor(differenceInMilisecond / 31536000000); var day_age = Math.floor((differenceInMilisecond % 31536000000) / 86400000); if ((today.getMonth() == birthday.getMonth()) && (today.getDate() == birthday.getDate())) { alert("Happy Birthday!!!"); } var month_age = Math.floor(day_age/30); day_age = day_age % 30; var tMnt= (month_age + (year_age*12)); var tDays =(tMnt*30) + day_age; if (isNaN(year_age) || isNaN(month_age) || isNaN(day_age)) { document.getElementById("age").innerHTML = ("Invalid birthday - Please try again!"); } else { document.getElementById("age").innerHTML = year_age + " years " + month_age + " months " + day_age + " days" + "<br/> or <br/> " + tMnt + " months " + day_age + " days" + "<br/> or <br/>" + tDays + " days" + "<br/> or <br/>" + tDays*24 + " hours" + "<br/> or <br/>" + tDays*24*3600 + " seconds" + "<br/> or <br/>" + tDays*24*3600*1000 + " miliseconds" ; } } |
Thank you for visiting!
We appreciate you for your precious time. I hope you are learn something from here! If you have any queries about this coding comment down below we will reply ASAP.
Keep coding, Keep debugging!