• We just launched and are currently in beta. Join us as we build and grow the community.

How to Calculate Your Age Base on Date Value in JavaScript

Boybeamer

Social Content Amplifier
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
69
Likes
181
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "contriner-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Calculate Your Age Base on Date Value</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <div

    class

    =

    "form-group"

    >
  19. <label

    >

    Birthdate</

    label

    >
  20. <input

    type

    =

    "date"

    class

    =

    "form-control"

    id

    =

    "birthday"

    placeholder

    =

    "Enter you birthdate"

    /

    >
  21. </

    div

    >
  22. <center><button

    class

    =

    "btn btn-primary btn-block"

    onclick

    =

    "calculateAge();"

    >

    Calculate</

    button

    ></

    center>
  23. </

    div

    >
  24. <div

    class

    =

    "col-md-8"

    >
  25. <center><h2

    class

    =

    "text-primary"

    >

    My Age</

    h2

    ></

    center>
  26. <div

    class

    =

    "alert-info"

    style

    =

    "padding:20px; border-radius:10px;"

    >
  27. <div

    id

    =

    "result"

    ><center><h1

    >

    NO DATA</

    h1

    ></

    center></

    div

    >
  28. </

    div

    >
  29. </

    div

    >
  30. </

    div

    >
  31. <script

    src

    =

    "script.js"

    ></

    script

    >
  32. </

    body

    >
  33. </

    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. if

    (

    date ===

    ""

    )

    {
  4. alert(

    "Please complete the required field!"

    )

    ;
  5. }

    else

    {
  6. let today =

    new

    Date

    (

    )

    ;
  7. let birthday =

    new

    Date

    (

    date)

    ;
  8. let year =

    0

    ;
  9. if

    (

    today.getMonth

    (

    )

    <

    birthday.getMonth

    (

    )

    )

    {
  10. year =

    1

    ;
  11. }

    else

    if

    (

    (

    today.getMonth

    (

    )

    ==

    birthday.getMonth

    (

    )

    )

    &&

    today.getDate

    (

    )

    <

    birthday.getDate

    (

    )

    )

    {
  12. year =

    1

    ;
  13. }
  14. let age =

    today.getFullYear

    (

    )

    -

    birthday.getFullYear

    (

    )

    -

    year;

  15. if

    (

    age <

    0

    )

    {
  16. age =

    0

    ;
  17. }
  18. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    "<center><h3>You are now <h1 style='color:red;'>"

    +

    age+

    "yrs old<h1></h3></center>"

    ;
  19. }
  20. }

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:

How%20to%20Calculate%20Your%20Age%20Base%20on%20Date%20Value%20in%20JavaScript%201_0.png


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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

338,631

338,639

Top