Skip to content

parsePartial

Parses an ISO date that may only be known to the year or the month: YYYY, YYYY-MM or YYYY-MM-DD. A partial date is anchored in the middle of its period, so it sorts and charts sensibly next to full dates:

  • YYYY → July 1st of that year
  • YYYY-MM → the 15th of that month
  • YYYY-MM-DD → that day

All dates are at 00:00:00.000 UTC. Returns null for anything else, including a month or day that does not exist (2025-13, 2025-02-30).

Available since v3.1.5

import { parsePartial } from '@helpers4/date';
// or, from the all-in-one package (same code, one install):
import { parsePartial } from 'helpers4/date';
parsePartial(input: string): PartialDate | null
ParameterTypeDescription
inputstringA `YYYY`, `YYYY-MM` or `YYYY-MM-DD` string

PartialDate | null — The anchored date with its precision, or null

A partial date is anchored in the middle of its period so it sorts next to full dates.

parsePartial('2008')    // => { date: 2008-07-01, precision: 'year' }
parsePartial('2008-03') // => { date: 2008-03-15, precision: 'month' }

A month or day that does not exist gives null.

parsePartial('2008-02-30') // => null

A partial ISO date resolved to a concrete Date, with the precision it had.

interface PartialDate {
  date: Date;
  precision: PartialDatePrecision;
}

Precision of a partial ISO date: which parts the input actually gave.

type PartialDatePrecision = 'year' | 'month' | 'day'

View source on GitHub