Validate a Gregorian date

Check that a year, a 1-based month and a day form a real calendar date.

JavaScript Version 1.0.0 · MIT · .md

validate-a-gregorian-date.js
function isValidDate({ year, month, day }) { const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); return day >= 1 && day <= [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]; }

Input & expected output

Input

{
  "year": 2024,
  "month": 2,
  "day": 29
}

Expected output

true

The edge case

Input

{
  "year": 2026,
  "month": 2,
  "day": 29
}

Expected output

false

Know the limits

  • The original used a zero-based month with new Date(y, m, 0), which reads the previous month’s length and accepted February 30. This version uses a 1-based month and explicit Gregorian rules.
  • The lab accepts years 1–9,999 and months 1–12.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Validate a Gregorian date from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Rewritten with a 1-based month and explicit Gregorian month lengths; the original zero-based month read the previous month's length and accepted February 30. Original MDX retained as inert source evidence.

Maintained by Oveyon. Original source commit: 14b481cf60df. This edition’s file: contents/validate-a-gregorian-date.mdx in akamind/1loc, commit 0a77f131b22d. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.