How to Calculate Your Age Base on Date Value in JavaScript

How to Calculate Your Age Base on Date Value in JavaScript

Introduction

In this tutorial we will create a How to Calculate Your Age Base on Date Value in JavaScript. This tutorial purpose is to teach how you to calculate your age base on the given date. This will cover all the important parts of the function that will calculate your age. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to system that needed to enter your personal information. I will give my best to provide you the easiest way of creating this program Calculate your age. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display form inputs and the result. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="contriner-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Calculate Your Age Base on Date Value</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <div class="form-group">
  18. <label>Birthdate</label>
  19. <input type="date" class="form-control" id="birthday" placeholder="Enter you birthdate"/>
  20. </div>
  21. <center><button class="btn btn-primary btn-block" onclick="calculateAge();">Calculate</button></center>
  22. </div>
  23. <div class="col-md-8">
  24. <center><h2 class="text-primary">My Age</h2></center>
  25. <div class="alert-info" style="padding:20px; border-radius:10px;">
  26. <div id="result"><center><h1>NO DATA</h1></center></div>
  27. </div>
  28. </div>
  29. </div>
  30. <script src="script.js"></script>
  31. </body>
  32. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will calculate your age base on the date you have entered in the textbox. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function calculateAge() {
  2. let date = document.getElementById('birthday').value;
  3.  
  4. if(date === ""){
  5. alert("Please complete the required field!");
  6. }else{
  7. let today = new Date();
  8. let birthday = new Date(date);
  9. let year = 0;
  10. if (today.getMonth() < birthday.getMonth()) {
  11. year = 1;
  12. } else if ((today.getMonth() == birthday.getMonth()) && today.getDate() < birthday.getDate()) {
  13. year = 1;
  14. }
  15. let age = today.getFullYear() - birthday.getFullYear() - year;
  16.  
  17. if(age < 0){
  18. age = 0;
  19. }
  20. document.getElementById('result').innerHTML = "<center><h3>You are now <h1 style='color:red;'>"+age+"yrs old<h1></h3></center>";
  21. }
  22. }

In this code we only create one method called calculateAge(), in the first line of code we bind the date value as variable named date. Next is we use a conditional statement to track if the value is empty, If the value is not empty it will call another line of codes.

We then set several variables to break down the date string format by today, birthday, year. To calculate your age we just simply use several conditional statement by comparing the birthdate, year, and month to get the expected age result.

Output:

The How to Calculate Your Age Base on Date Value in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Calculate Your Age Base on Date Value in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment