A simple program to calculate the age of the person using JavaScript programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Please subscribe to my channel  https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg

=================================================



Want to support my channel?

GCash Account

Jake Pomperada

09173084360



Paypal

https://paypal.me/jakerpomperada



Patreon

https://www.patreon.com/jakerpomperada





Thank you very much for your support.
Program Listing

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Age Calculator in JavaScript</title>
    <style>
        *,html {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        body {
            background: #fefefe;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            font-family: sans-serif;
            font-size: 110%;
        }

        h1, h3 { text-align: center; }
        h3 { margin-bottom: 40px; }

        form {
            display: flex;
            flex-flow: wrap;
            justify-content: space-between;
            max-width: 500px;
            width: 100%;
        }

        .block {
            flex-grow: 1;
            width: 150px;
            text-align: center;
        }

        .block:last-of-type {
            margin: 0;
        }
        
        select, input, button, .output {
            display: block;
            width: 100%;
            border: none;
            outline: none;
            padding: 10px;
            background: #000;
            color: #fff;
            cursor: pointer;
            font-size: 15px;
            height: 40px;
            text-align: center;
        }

        button, .output {
            background: #673ab7;
            font-size: 110%;
            flex-grow: 3;
            width: 100%;
        }
        
        .output {
            background-color: aqua;
            color: #000;
            cursor: auto;
        }
    </style>
</head>
<body>
    <div class="text">
        <h1>Simple Age Calculator in JavaScript</h1>
        <h3>Jake R. Pomperada, MAED-IT, MIT</h2>
    </div>
    <form>
        <div class="block">
            <label for="month">Month</label>
            <select id="month" required>
                <option value="01">January</option>
                <option value="02">February</option>
                <option value="03">March</option>
                <option value="04">April</option>
                <option value="05">May</option>
                <option value="06">June</option>
                <option value="07">July</option>
                <option value="08">August</option>
                <option value="09">September</option>
                <option value="10">October</option>
                <option value="11">November</option>
                <option value="12">December</option>
            </select>
        </div>

        <div class="block">
            <label for="day">Day</label>
            <select id="day" required>
                <option value="01">1</option>
                <option value="02">2</option>
                <option value="03">3</option>
                <option value="04">4</option>
                <option value="05">5</option>
                <option value="06">6</option>
                <option value="07">7</option>
                <option value="08">8</option>
                <option value="09">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>
            </select>
        </div>

        <div class="block">
            <label for="year">Year</label>
            <input type="number" min="1000" max="2022" id="year" value="2022" required>
        </div>

        <button type="submit" id="btnCalculate">Calculate</button>
    </form>
    
    <script>
        document.querySelector('form').onsubmit  = (e) => {
            e.preventDefault()

            // Extract and collect data
            let month  = parseInt(document.querySelector("#month").value),
                day    = parseInt(document.querySelector("#day").value),
                year   = parseInt(document.querySelector("#year").value),
                button = document.querySelector("#btnCalculate"),
                output = document.querySelector(".output"),

                // Today's date
                today  = new Date(),

                // Birthdate
                bdate   = new Date(year, month - 1, day),
                
                // Calculate the difference of dates
                diff = today.valueOf() - bdate.valueOf(),

                // Get year/s
                ageYear = Math.floor(diff / 31536000000),

                // Get day/s then convert to milliseconds
                calcDayMs = Math.floor((diff % 31536000000) / 86400000),

                // Get month/s
                ageMonth = Math.floor(calcDayMs / 30)

                // Get day/s
                ageDay = calcDayMs % 30,
                bdayInfo = "Years:"+ageYear+" Months:"+ageMonth+" Days:"+ageDay

            const elem = document.createElement('div')
            elem.className = "output"

            if (bdate > today) {
                elem.innerText = "Invalid date selected - Please try again!"
            } else {
                elem.innerText = bdayInfo
            }

            // Remove if output element exists
            output ? output.remove() : null;

            // Post result below Calculate button
            button.after(elem)
        };
    </script>
</body>
</html>