HTML.form.guide

age calculation code

Simplest method - Age Calculator Code for Your website

Suppose you have a requirement to capture the date of birth of the user and then calculate age from the date. You can do the calculation using the Date Javascript global object. Here is some sample code that does just that. function calculateAge(date) { const now = new Date(); const diff = Math.abs(now - date ); const age = Math.floor(diff / (1000 * 60 * 60 * 24 * 365)); return age } In this function, the age calculation is straightforward; it finds the difference between the dates.

Continue Reading →