This JavaScript code snippet helps you to calculate age in years, months, and days. It gets the date of birth from the users and the date as far as age should be counted, calculates it, and shows the result in years, months, and days format.
Basically, you don’t need to add the current date as it automatically inserts today’s date. On the other hand, if a user wants to calculate his/her age in the future, then this feature is quite helpful.
This age calculator is beneficial for counting the ages until a certain date. Whether it is the date of the past or the future, this age calculator shows us the age in the form of years, months, and days in the blink of an eye.
How to Calculate Age in Years, Months, and Days in JavaScript
1. In order to create an age calculator, we need to create number inputs for the date of birth and date as far age should be counted. So, create these inputs with the following mentioned attributes and ids. Wrap all code into a div element and define its id “container”.
<div id="container"> <img src="https://lh6.ggpht.com/wLfwA7MS68Zd2KedA1vCX6-zLqDIUSwBC3W4GzgJaZfOWX2BOlw6mjj0DatL4oII8Q=w300" alt="" /> <form id="form" autocomplete="off"> <h5>All Must Be In Numbers Other Wise We Cannot Calculate.</h5> <label for="date"> Date Of Birth: <input type="text" maxlength="2" size="2" id="date" placeholder="Date" required/> <input type="text" maxlength="2" size="2" id="month" placeholder="Month" autocomplete="on" required/> <input type="text" maxlength="4" size="4" id="year" placeholder="Year" required/> </label> <br /> <br /> <label for="date2"> Today's Date: <input type="text" maxlength="2" size="2" id="date2" placeholder="Date"/> <input type="text" maxlength="2" size="2" id="month2" placeholder="Month"/> <input type="text" maxlength="4" size="4" id="year2" placeholder="Year"/> </label> <br /> <br /> <button id="calbtn">Calculate</button> <br /> <br /> <span id="age"></span> <span id="months"></span> <span id="days"></span> </form> </div>
2. After that, style the calculator using the following CSS styles. You can also define your own CSS in order to customize the age calculator according to your needs.
#container { text-align: center; padding: 20px; } #form{ margin-top: 20px; } input[type='text'] { padding: 5px 10px; border: 1px solid #bdc3c7; outline: 0; color: #2980b9; font-weight: bold; } #calbtn { border: 0; padding: 10px 20px; cursor: pointer; background: #d35400; color: #f1c40f; font-size: 20px; outline: 0; } #calbtn:hover, #calbtn:active { background-color: #c0392b; outline: 0; } span { display: inline-block; font-size: 45px; font-weight: bold; color: #27ae60; }
3. Finally, add the following JavaScript function for the age calculator between the <script> tag before closing the body tag and done.
var form = document.getElementById("form"), bdate = document.getElementById("date"), bmonth = document.getElementById("month"), byear = document.getElementById("year"), date = document.getElementById("date2"), month = document.getElementById("month2"), year = document.getElementById("year2"), age = document.getElementById("age"), days = document.getElementById("days"), mons = document.getElementById("months"), today = new Date(); year.value = today.getFullYear(); month.value = today.getMonth() + 1; date.value = today.getDate(); form.addEventListener('submit', function(event) { event.preventDefault(); var by = Number.parseFloat(byear.value), bm = Number.parseFloat(bmonth.value), bd = Number.parseFloat(bdate.value), ty = Number.parseFloat(year.value), tm = Number.parseFloat(month.value), td = Number.parseFloat(date.value); if (td < bd) { days.innerHTML = (td - bd + 30) + ' days'; tm = tm - 1; } else { days.innerHTML = (td - bd) + ' days' } if (tm < bm) { months.innerHTML = (tm - bm + 12) + ' months'; ty = ty - 1; } else { months.innerHTML = (tm - bm) + ' months' } age.innerHTML = "Age: " + (ty - by) + ' years'; });
That’s all! Hopefully, you have successfully integrated the age calculator into your project. If you have any questions or facing any issues, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.