Creating Age Calculator in HTML CSS and JavaScript

 

<!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>Document</title>

    <style>

        body{

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

        }

        .container{

            

            width: 500px;

            margin: 10px auto;

            background-color: #eee;

            border-radius: 25px;

        }

        .base{

            width: 100%;

            margin: 0;

            overflow: hidden;

            display: block;

            float: none;


        }

        .block{

            width: 100px;

            padding:5px 20px;

            margin-left: 25px;

            display: inline-block;

            float: left;


        }

        .base h4{

            font-size: 26px;

            text-align: center;

            font-family: sans-serif;

            font-weight: bolder;

            

            box-shadow: 0 2px #bababa;

            background-color:rgb(75, 73, 73);

            font-size: 34px;

               color:burlywood;

        }



        .title{

            font-size: 20px;

            text-align: left;

            font-family: sans-serif;

            font-weight: normal;

            line-height: 0.5;

            letter-spacing: 0.5px;

            word-spacing: 2.7px;

            color: #1073d0;

        }

        input[type=text]{

            width: 80px;

            margin: auto;

            outline: none;

            min-height: 20px;

            border: 2px solid #1073d0;

            padding: 10px;

            background-color: #f7f7f7;

            border-radius: 5px;

            color: #1073d0;

            font-size: 20px;


        }

        input[type=text]:focus{

            background-color: #ffffff;

            border: 2px solid rgb(197, 171, 123);

            outline: none;


        }

        input[type=button]{

            width: 150px;

            margin-left: 35%;

            margin-top: 40px;

            outline: none;

            border: none;

            background-color:rgb(75, 73, 73);

            color: #ffffff;

            padding: 14px 16px;

            text-align: center;

            font-size: 16px;

            cursor: pointer;


        }

        input[type=button]:hover{

            background-color: #003669;

        }

        #age{

            text-align: center;

            display: block;

            margin: 10px;

            margin-top: 20px;

            padding: 10px;

            padding-bottom: 20px;

            overflow: hidden;

            font-family: Verdana, Geneva, Tahoma, sans-serif;

            font-size: 23px;

            font-weight: normal;

            line-height: 1.5;

            word-spacing: 2.7px;

            color: navy;

        }

    </style>

</head>

<body>

     <div class="container">

         <form action="">

             <div class="base">

                 <div class="enter">

                     <h4>Age calculator</h4>

                 </div>

                 <div class="block">

                     <p class="title">

                         Date

                     </p>

                     <input type="text" name="date" id="date" placeholder="dd" required minlenght="1" maxlenght="2">

                 </div>

                 <div class="block">

                     <p class="title">Month</p>

                     <input type="text" name="month" id="month" placeholder="mm" required minlenght="1" maxlenght="2">


                 </div>

                 <div class="block">

                     <p class="title">Year</p>

                     <input type="text" name="year" id="year" placeholder="yyyy" required minlenght="1" maxlenght="2">

                 </div>

             </div>

             <div class="base">

                 <div class="enter">

                     <input type="button" name="submit" value="Submit" onclick="age()"/>

                 </div>

             </div>

             <div id="age"></div>

         </form>

     </div>

    

     <script>

         function age(){

             var d1=document.getElementById('date').value;

             var m1=document.getElementById('month').value;

             var y1=document.getElementById('year').value;


             var date = new Date();

             var d2=date.getDate();

             var m2= 1+date.getMonth();

             var y2=date.getFullYear();


             var 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;

             }

             var d=d2-d1;

             var m= m2-m1;

             var y=y2-y1;


             document.getElementById('age').innerHTML="your age is "+y+" Years "+m+" Months "+d+" Days";

         }

     </script>

</body>

</html>

Comments

Blogs

LightBox image Galery in html css and javaScript