Age Calculator using JavaScript | Coding Torque


 Age Calculator using JavaScript

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

Let's go step by step💥

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> 
Explaination: In the above HTML Code, we have imported font-awesome icons from cdnjs and google-fonts. Next in the body tag we have set up the inputs to take the inputs from users. Next a button on which there is onclick event listener to calculate the age once user click submit. 

CSS Code

* {
    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;
}

JavaScript Code

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`;
}

If you have any doubt or any project idea feel free to Contact Us

Hope you find it helpful💖

Written by : Coding Torque | Piyush Patil

Follow us on instagram  for more projects like this👨‍💻
- @code.scientist
- @codingtorque
- @codingknights

Post a Comment

Previous Post Next Post