Calculate Age from DoB (Date of Birth)

When the date of birth (DOB) is available it looks very straightforward to calculate age programmatically, but it isn't always accurate due to the complexity of handling leap years. Advanced programming languages also don't provide relevant functionality out-of-the-box because of many variations in logic available on the internet. Deviation in calculating age also gets impacted due to the difference in timezone between the client machine and application server.

The easiest method to accurately calculate the age without worrying about leap years is explained below:

Age = First 3 bytes of the result obtained by subtracting DOB from Today's date.

Example 1: If an employee's DOB is 11th May 2000 (20000511) and Today's date is 11th May 2018 (20180511) then Age is 18.

Calculation is 2180511 - 2000511 = 0180000

Example 2: If an employee's DOB is 15th May 2000 (20000515) and Today's date if 14th May 2018 (20180514) then Age is 17.

Calculation is 2180514 - 2000515 = 0179999

Example 3: If an employee's DOB is 12th May 1998 (19980512) and Today's date if 16th May 2018 (20180516) then Age is 19.

Calculation is 2180516 - 1980512 = 200004

No comments:

Post a Comment