Age Calculator using JavaScript | Coding Torque

Share your love
Hello Guys! Welcome to Coding Torque. In this blog we are going to create Age Calculator using JavaScript😍.

Before we start, here are some more JavaScript Games you might like to create:

1. Snake Game using JavaScript

2. 2D Bouncing Ball Game using JavaScript

3. Rock Paper Scissor Game using JavaScript

4. Tic Tac Toe Game using JavaScript

5. Whack a Mole Game using JavaScript

 

I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.

 

HTML Code 

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome Icons  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
        integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
        crossorigin="anonymous" />

    <!-- Google Fonts  -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">

    <title>Age Calculator using javascript - @code.scientist x @codingtorque</title>
</head>

<body>
    <div class="calculator-form">
        <h1>Age Calculator</h1>
        <div class="inputs">
            <div class="block">
                <label> for="date">Date</label>
                <input type="number" max="31" id="date" class="input" placeholder="dd">
            </div>
            <div class="block">
                <label> for="month">Month</label>
                <input type="number" max="12" id="month" class="input" placeholder="mm">
            </div>
            <div class="block">
                <label> for="year">Year</label>
                <input type="number" id="year" class="input" placeholder="yyyy">
            </div>
        </div>
        <button> type="submit" class="btn" onclick="calculateAge()">Submit</button>
        <p> id="displayAge"></p>
    </div>
</body>

</html>

Output Till Now

 

CSS Code 

Create a file style.css and paste the code below.

* {
    font-family: 'Poppins', sans-serif;
}

body {
    background-color: #111827;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
}

.calculator-form {
    display: flex;
    flex-direction: column;
    align-items: center;
    border: 2px solid deepskyblue;
    padding: 40px;
    border-radius: 20px;
}

.inputs {
    display: flex;
}

.block {
    display: flex;
    flex-direction: column;
    margin: 0 8px;
}

label {
    font-size: 18px;
}

.input {
    border: 2px solid deepskyblue;
    height: 3rem;
    width: 8rem;
    border-radius: 7px;
    padding: 8px;
    margin-top: 4px;
    outline: none;
}

.btn {
    height: 3rem;
    width: 8rem;
    border: 2px solid deepskyblue;
    background: deepskyblue;
    color: white;
    font-size: 16px;
    border-radius: 7px;
    padding: 8px;
    margin-top: 30px;
}

Output Till Now

 

JavaScript Code 

Create a file script.js and paste the code below.
const calculateAge = () => {
let d1 = document.getElementById("date").value;
let m1 = document.getElementById("month").value;
let y1 = document.getElementById("year").value;

let date = new Date();
let d2 = date.getDate();
let m2 = 1 + date.getMonth();
let y2 = date.getFullYear();
let month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

if (d1 > d2) {
    d2 = d2 + month[m2 - 1]
    m2 = m2 - 1;
}

if (m1 > m2) {
    m2 = m2 + 12;
    y2 = y2 - 1;
}

let d = d2 - d1;
let m = m2 - m1;
let y = y2 - y1;

document.getElementById("displayAge").innerText = `Your Age is ${y} Years, ${m} Months and ${d} Days`;
}

 

Written by: Piyush Patil
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpful💖
Share your love