Javascript Calculate Age from Date of Birth - Step by Step Code Explanation

preview_player
Показать описание
Calculate Age From Date of Birth DOB in Vanilla JavaScript

Javascript Calculate Age, Calculate Age Javascript
Vanilla Javascript Solution to Calculate Age in Years Months Days from Date of Birth [DOB]

Related Tags: calculate age from date of birth javascript, get age from date of birth javascript, javascript age, javascript calculate age from date, calculate age based on date of birth using javascript, javascript calculate age, Calculate Age JS

Javascript Calculate Age – Introduction
In this article, We will learn how to Calculate a persons age using their Date of Birth as an input in Javascript. Vanilla Javascript solution to return a persons age using their DOB
Pre-requisites: Basic HTML and Javascript Knowledge

Video Tutorial – Javascript Calculate Age from Date of Birth

Now, you have see the video tutorial, lets, move on to the code explanation and we will cover the following -.

How to Calculate Age Javascript in Years, Months, Days, Hours, Minutes, Seconds and Milliseconds using persons DOB
Before we move on to Javascript code, lets create a HTML div to help us print results on the screen, you can replace this with your GUI.

HTML Code and Explanation

In the HTML File.

I create a div with an id called placeholder.

Later this id will be used in JavaScript to identify this div element and print results on the screen.
Try the code for yourself here: CodePen

Calculate Age JavaScript Code and Explanation
In the JS File

First I define a variable called DOB to store a persons Date of Birth.

Definition and Usage

The parse() method parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970

Using these functions as base.

I create 2 new variables millisecondsBetweenDOBAnd1970 and millisecondsBetweenNowAnd1970 to store number of milliseconds between DOB/NOW and January 1, 1970 respectively.

Then to get the age of a person in Milliseconds subtract DOB from NOW.

Now, when I have a persons age in Milliseconds, we just need to write basic logic to convert it into Years, Months, Days, Hours, Minutes, Seconds

As we know this as facts:

1 year = 365 day (except leap year), 1 Month = 30 days (approx.), 1 Day = 24 Hours, 1 Hour = 60 Minutes, 1 Minute = 60 Seconds, 1 Second = 1000 Milliseconds

Disclaimer: Leap Year hasn’t been taken into consideration in this example. The days in a month has also been generalised to 30 days in a months (whereas depending on a year a month can have 31, 30, 29 or 28 days in it)

Please do not use this article for your college projects unless the above disclaimer is not a concern and don’t forget to reference this page.

Lets, get back into explanation, then I convert these facts into code.

Output / Result
In the results you can see all the conversions, improve or try the code for yourself on CodePen.

Please don’t forget to leave a comment or questions, you can easily signup/login to the website using Email, Facebook, Twitter or Google Account.
Рекомендации по теме
Комментарии
Автор

Hi, really appreciated your run-through on the age converter. I started coding in Nov 22', and am at the stage where I'm trying to wrap my head around JavaScript concepts. So your detailed explanation was super helpful! Thanks!

PipTrader.
Автор

I think the way that you made an adjustment for a birthday before 1970 is very clever and is not on the wc3 example.
But I must know why did you not continue with Math.round for days and months, when I applied it the results were much more accurate for those fields. Not that it really matters.
Also I noticed It will work for any year including BCE by ex. -0025 . for BC 25
I would like to know is there anyway to have results come in an alert or maybe just a pop up window? I will have to do more research to figure this out, Thanks for the great head start!

teramike
Автор

How can i do this, but instead of writing "March 1, 2002" (for example) i will write "1-03-2002"??

jonasverkaap
Автор

How can I change the Year number in a longer number ? For example - we had the Age in Years : 24 and now i want this : 24.7319403890 which is constantly changing, where ofc the last 4 number change every second and the numbers backwards gets higher and higher by the time

bernhardtrian
Автор

I didn't understand the logic, finding difference of age In milliseconds

karthikjmoger
Автор

plz give the link of the agecalculator html code

asnhelp
Автор

Sir pls hindi Or punjabi ch bnaa dya kro vdoz

navdeepbains