Skip to content
This repository was archived by the owner on Jun 19, 2026. It is now read-only.

Latest commit

 

History

History
24 lines (17 loc) · 821 Bytes

File metadata and controls

24 lines (17 loc) · 821 Bytes

calculateAge()

Overview

Calculates the age based on a given birth date.

Code

A screenshot of the titular code snippet in my development environment.

const calculateAge = (birthDate) => {
  const currentDate = new Date();
  const yearDifference = currentDate.getFullYear() - birthDate.getFullYear();
  const monthDifference = currentDate.getMonth() - birthDate.getMonth();
  const dayDifference = currentDate.getDate() - birthDate.getDate();

  return monthDifference < 0 || (monthDifference === 0 && dayDifference < 0)
    ? yearDifference - 1
    : yearDifference;
};