# How to calculate age in JavaScript with demo

Javascript calculate age: Here is this tutorial we are going to learn how we can calculate age from the given Date of Birth (DOB) in javascript.  I have a requirement where I have to calculate the user's age based on its DOB. To check user age eligibility and apply respective validation.

For example on the registration page if the user's age is less than 18, then we add validation by which user can't create an account. So based on DOB I can disable or enable the submit button respectively.

### 2 ways to calculate age in Javascript

1. Calculate age from date of birth to current date.
2. Calculate the age between the given two dates.

## Calculate age from date of birth to current date

Here in this method, we add an input textbox and button tag. On button click, we call javascript function which gets the date from the input textbox and calculates age.

Our HTML code looks like as below:

``````<input type="date" id="txtDOB" />
<button id="btnCalculate"  onclick="fnCalculateAge();" >Calculate Age</button>``````

Here's to calculate age from date of birth JavaScript function is as follow:

`````` function fnCalculateAge(){

var userDateinput = document.getElementById("txtDOB").value;
console.log(userDateinput);

// convert user input value into date object
var birthDate = new Date(userDateinput);
console.log(" birthDate"+ birthDate);

// get difference from current date;
var difference=Date.now() - birthDate.getTime();

var  ageDate = new Date(difference);
var calculatedAge=   Math.abs(ageDate.getUTCFullYear() - 1970);
}``````

## # Calculate the age between given two date.

Here we going to add two textboxes i.e start date and end date. On button click, we call js function fnCalculateAgeFromGivesDates.

Our HTML markup looks like as written below:

``````<input type="date" id="txtStartDate" />
<input type="date" id="txtEndDate" />
<button id="btnCalculate2"  onclick="fnCalculateAgeFromGivesDates();" >Calculate Age</button>``````

JS code to calculate age from two dates is as follow:

`````` function fnCalculateAgeFromGivesDates(){
var startDateInput = document.getElementById("txtStartDate").value;
var endDateInput = document.getElementById("txtEndDate").value;

// convert user input values into date object
var startDate = new Date(startDateInput);
var endDate  = new Date(endDateInput);

// get difference between two date;
var difference=endDate - startDate.getTime();

var  ageDate = new Date(difference);
var calculatedAge=   Math.abs(ageDate.getUTCFullYear() - 1970);
}``````
Conclusion: Here in this article we learn how to calculate age from any given date. Also how to determine age from date of birth (DOB).  With above 2 demos we provided calculate age javascript function.
{"ft_lang_label":"__label__en","ft_lang_prob":0.68443185,"math_prob":0.9601205,"size":2779,"snap":"2021-43-2021-49","text_gpt3_token_len":612,"char_repetition_ratio":0.13585585,"word_repetition_ratio":0.06870229,"special_character_ratio":0.22094278,"punctuation_ratio":0.13389121,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9983897,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-15T20:30:10Z\",\"WARC-Record-ID\":\"<urn:uuid:4e02ce18-a13f-4973-82ce-7404dbddcea0>\",\"Content-Length\":\"118687\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:49afceae-305f-41a2-801e-804b4dadf163>\",\"WARC-Concurrent-To\":\"<urn:uuid:e83c7842-be0f-4d08-899b-1664a22bb38c>\",\"WARC-IP-Address\":\"3.211.102.226\",\"WARC-Target-URI\":\"https://codepedia.info/calculate-age-in-javascript\",\"WARC-Payload-Digest\":\"sha1:7ZFZWDTPXUVDK5MSDEXZSTCX3JKMTVDH\",\"WARC-Block-Digest\":\"sha1:OVRZWNQKIV3UTLAEOLW4FLWV4U3T5VDU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323583083.92_warc_CC-MAIN-20211015192439-20211015222439-00019.warc.gz\"}"}