Skip to content

formatPartial

Formats a partial ISO date at the precision it was given, in UTC: a year alone is approximate and reads c. 2008, a month Mar 2008 and a full day Mar 9, 2008.

Month names and their order follow locale. The marker before a year alone does not: see FormatPartialOptions.

Returns null when parsePartial would, or when the locale is invalid.

Available since v3.1.5

import { formatPartial } from '@helpers4/date';
// or, from the all-in-one package (same code, one install):
import { formatPartial } from 'helpers4/date';
formatPartial(input: string, options: FormatPartialOptions): string | null
ParameterTypeDescription
inputstringA `YYYY`, `YYYY-MM` or `YYYY-MM-DD` string
optionsFormatPartialOptionsLocale and approximate-year marker — see FormatPartialOptions

string | null — The label, or null

An approximate year reads “c. 2008”, a month “Mar 2008”, a day “Mar 9, 2008”.

formatPartial('2008')       // => 'c. 2008'
formatPartial('2008-03')    // => 'Mar 2008'
formatPartial('2008-03-09') // => 'Mar 9, 2008'

The locale sets the month names; the marker for a year alone is a word, so you give it.

formatPartial('2008', { locale: 'fr', approximate: 'vers' }) // => 'vers 2008'
formatPartial('2008-03-09', { locale: 'fr' })                // => '9 mars 2008'

Returns null instead of a wrong label.

formatPartial('2008-13') // => null

Options for formatPartial.

interface FormatPartialOptions {
  approximate?: string;
  locale?: string;
}

View source on GitHub