mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
fix(settings): let every date follow a date format of the user's choice
Every date in TREK was written in the interface language's format, and English is en-US, so an English interface always showed and read mm/dd/yyyy with no way to change it. A few native date inputs followed the browser instead, so one screen could mix two formats. date_format is now a user setting with an admin default, next to the time format: automatic (the language's format, as before), YYYY-MM-DD, YYYY/MM/DD, DD.MM.YYYY, DD/MM/YYYY or MM/DD/YYYY. The date picker shows, pre-fills, hints and reads typed dates in it, and every date written in numbers across the app (lists, cards, admin panels, CSV and PDF exports) goes through one formatter. Dates written with a month name keep the language's wording. The remaining native date inputs are the shared picker now. The display settings MCP tool reads and writes the key. Closes #2971
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
// FE-UTIL-DATEFMT-001 to FE-UTIL-DATEFMT-020
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
dateFieldOrder,
|
||||
dateFormatOptions,
|
||||
dateLayout,
|
||||
displayDateFormatter,
|
||||
formatDisplayDate,
|
||||
formatDisplayDateTime,
|
||||
isNumericDate,
|
||||
} from './dateFormat'
|
||||
|
||||
// 31 December 2026, 15:04 UTC: no day of it reads as a month, so every layout is unambiguous.
|
||||
const NYE = new Date(Date.UTC(2026, 11, 31, 15, 4))
|
||||
const UTC = { timeZone: 'UTC' } as const
|
||||
const NUMERIC: Intl.DateTimeFormatOptions = { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' }
|
||||
|
||||
describe('dateFormat (#2971)', () => {
|
||||
describe('formatDisplayDate', () => {
|
||||
it('FE-UTIL-DATEFMT-001: auto, an unknown value or none is exactly toLocaleDateString', () => {
|
||||
const cases: [string, Intl.DateTimeFormatOptions | undefined][] = [
|
||||
['en-US', undefined],
|
||||
['en-US', NUMERIC],
|
||||
['de-DE', NUMERIC],
|
||||
['ko-KR', { day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' }],
|
||||
['en-GB', { day: '2-digit', month: '2-digit', year: '2-digit', timeZone: 'UTC' }],
|
||||
]
|
||||
for (const [locale, opts] of cases) {
|
||||
const expected = NYE.toLocaleDateString(locale, opts)
|
||||
expect(formatDisplayDate(NYE, locale, opts, 'auto')).toBe(expected)
|
||||
expect(formatDisplayDate(NYE, locale, opts, 'DD-MM-YY')).toBe(expected)
|
||||
expect(formatDisplayDate(NYE, locale, opts, null)).toBe(expected)
|
||||
expect(formatDisplayDate(NYE, locale, opts)).toBe(expected)
|
||||
}
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-002: each format writes 31 December 2026 in its own layout', () => {
|
||||
expect(formatDisplayDate(NYE, 'en-US', UTC, 'YYYY-MM-DD')).toBe('2026-12-31')
|
||||
expect(formatDisplayDate(NYE, 'en-US', UTC, 'YYYY/MM/DD')).toBe('2026/12/31')
|
||||
expect(formatDisplayDate(NYE, 'en-US', UTC, 'DD.MM.YYYY')).toBe('31.12.2026')
|
||||
expect(formatDisplayDate(NYE, 'en-US', UTC, 'DD/MM/YYYY')).toBe('31/12/2026')
|
||||
expect(formatDisplayDate(NYE, 'de-DE', UTC, 'MM/DD/YYYY')).toBe('12/31/2026')
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-003: the language does not change a chosen format', () => {
|
||||
for (const locale of ['en-US', 'de-DE', 'ko-KR', 'ar', 'fr-FR']) {
|
||||
expect(formatDisplayDate(NYE, locale, NUMERIC, 'YYYY-MM-DD')).toBe('2026-12-31')
|
||||
}
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-004: single-digit days and months are padded', () => {
|
||||
const march = new Date(Date.UTC(2026, 2, 5))
|
||||
expect(formatDisplayDate(march, 'en-US', { day: 'numeric', month: 'numeric', year: 'numeric', timeZone: 'UTC' }, 'DD.MM.YYYY')).toBe('05.03.2026')
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-005: year 2-digit keeps two digits of the year', () => {
|
||||
const opts: Intl.DateTimeFormatOptions = { day: '2-digit', month: '2-digit', year: '2-digit', timeZone: 'UTC' }
|
||||
expect(formatDisplayDate(NYE, 'en-US', opts, 'DD.MM.YYYY')).toBe('31.12.26')
|
||||
expect(formatDisplayDate(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe('26-12-31')
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-006: day and month without a year write only those two, in layout order', () => {
|
||||
const opts: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'numeric', timeZone: 'UTC' }
|
||||
expect(formatDisplayDate(NYE, 'en-US', opts, 'DD.MM.YYYY')).toBe('31.12')
|
||||
expect(formatDisplayDate(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe('12-31')
|
||||
expect(formatDisplayDate(NYE, 'en-US', opts, 'MM/DD/YYYY')).toBe('12/31')
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-007: a month name or a long dateStyle is left to Intl', () => {
|
||||
const named: Intl.DateTimeFormatOptions[] = [
|
||||
{ weekday: 'short', day: 'numeric', month: 'short', timeZone: 'UTC' },
|
||||
{ day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' },
|
||||
{ month: 'long', year: 'numeric', timeZone: 'UTC' },
|
||||
{ dateStyle: 'medium', timeZone: 'UTC' },
|
||||
]
|
||||
for (const opts of named) {
|
||||
expect(formatDisplayDate(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(NYE.toLocaleDateString('de-DE', opts))
|
||||
}
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-021: a weekday beside a numeric date keeps the language, the date takes the format', () => {
|
||||
const opts: Intl.DateTimeFormatOptions = { weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' }
|
||||
expect(formatDisplayDate(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(`${NYE.toLocaleDateString('de-DE', { weekday: 'short', timeZone: 'UTC' })}, 2026-12-31`)
|
||||
expect(formatDisplayDate(NYE, 'de-DE', opts, 'auto')).toBe(NYE.toLocaleDateString('de-DE', opts))
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-008: dateStyle short counts as numeric', () => {
|
||||
expect(formatDisplayDate(NYE, 'en-US', { dateStyle: 'short', timeZone: 'UTC' }, 'DD/MM/YYYY')).toBe('31/12/2026')
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-009: an invalid date falls back to toLocaleDateString', () => {
|
||||
const invalid = new Date('nope')
|
||||
expect(formatDisplayDate(invalid, 'en-US', NUMERIC, 'YYYY-MM-DD')).toBe(invalid.toLocaleDateString('en-US', NUMERIC))
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatDisplayDateTime', () => {
|
||||
it('FE-UTIL-DATEFMT-010: auto, an unknown value or none is exactly toLocaleString', () => {
|
||||
const opts: Intl.DateTimeFormatOptions = { ...NUMERIC, hour: '2-digit', minute: '2-digit' }
|
||||
for (const locale of ['en-US', 'de-DE']) {
|
||||
expect(formatDisplayDateTime(NYE, locale, opts, 'auto')).toBe(NYE.toLocaleString(locale, opts))
|
||||
expect(formatDisplayDateTime(NYE, locale, opts, 'nope')).toBe(NYE.toLocaleString(locale, opts))
|
||||
expect(formatDisplayDateTime(NYE, locale, UTC)).toBe(NYE.toLocaleString(locale, UTC))
|
||||
}
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-011: the date follows the format, the time of day stays Intl', () => {
|
||||
const opts: Intl.DateTimeFormatOptions = { ...NUMERIC, hour: '2-digit', minute: '2-digit' }
|
||||
const time = NYE.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' })
|
||||
expect(formatDisplayDateTime(NYE, 'en-US', opts, 'YYYY-MM-DD')).toBe(`2026-12-31 ${time}`)
|
||||
const hour12 = { ...opts, hour12: false }
|
||||
const time24 = NYE.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: false, timeZone: 'UTC' })
|
||||
expect(formatDisplayDateTime(NYE, 'en-US', hour12, 'DD.MM.YYYY')).toBe(`31.12.2026 ${time24}`)
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-012: without options it writes the date and the default time', () => {
|
||||
expect(formatDisplayDateTime(NYE, 'en-US', UTC, 'DD/MM/YYYY')).toBe(
|
||||
`31/12/2026 ${NYE.toLocaleTimeString('en-US', UTC)}`
|
||||
)
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-013: time-only options are passed through untouched', () => {
|
||||
const opts: Intl.DateTimeFormatOptions = { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' }
|
||||
expect(formatDisplayDateTime(NYE, 'de-DE', opts, 'YYYY-MM-DD')).toBe(NYE.toLocaleString('de-DE', opts))
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-014: date-only options give just the date, named dates stay Intl', () => {
|
||||
expect(formatDisplayDateTime(NYE, 'en-US', NUMERIC, 'YYYY/MM/DD')).toBe('2026/12/31')
|
||||
const named: Intl.DateTimeFormatOptions = { weekday: 'short', day: 'numeric', month: 'short', hour: '2-digit', timeZone: 'UTC' }
|
||||
expect(formatDisplayDateTime(NYE, 'en-US', named, 'YYYY-MM-DD')).toBe(NYE.toLocaleString('en-US', named))
|
||||
})
|
||||
})
|
||||
|
||||
describe('displayDateFormatter', () => {
|
||||
it('FE-UTIL-DATEFMT-015: auto behaves like Intl.DateTimeFormat', () => {
|
||||
const intl = new Intl.DateTimeFormat('en-US', NUMERIC)
|
||||
expect(displayDateFormatter('en-US', NUMERIC, 'auto').format(NYE)).toBe(intl.format(NYE))
|
||||
expect(displayDateFormatter('en-US', NUMERIC).format(NYE.getTime())).toBe(intl.format(NYE))
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-016: numeric dates follow the format, for Date and timestamp alike', () => {
|
||||
const f = displayDateFormatter('en-US', NUMERIC, 'DD.MM.YYYY')
|
||||
expect(f.format(NYE)).toBe('31.12.2026')
|
||||
expect(f.format(NYE.getTime())).toBe('31.12.2026')
|
||||
const withTime = displayDateFormatter('en-US', { ...NUMERIC, hour: '2-digit', minute: '2-digit' }, 'YYYY-MM-DD')
|
||||
expect(withTime.format(NYE)).toBe(
|
||||
`2026-12-31 ${NYE.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', timeZone: 'UTC' })}`
|
||||
)
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-017: a named month is left to Intl', () => {
|
||||
const opts: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', timeZone: 'UTC' }
|
||||
expect(displayDateFormatter('de-DE', opts, 'YYYY-MM-DD').format(NYE)).toBe(new Intl.DateTimeFormat('de-DE', opts).format(NYE))
|
||||
})
|
||||
})
|
||||
|
||||
describe('dateFieldOrder, dateLayout and isNumericDate', () => {
|
||||
it('FE-UTIL-DATEFMT-018: a chosen format sets the order, auto asks the language', () => {
|
||||
expect(dateFieldOrder('en-US', 'DD.MM.YYYY')).toEqual(['day', 'month', 'year'])
|
||||
expect(dateFieldOrder('de-DE', 'MM/DD/YYYY')).toEqual(['month', 'day', 'year'])
|
||||
expect(dateFieldOrder('en-US', 'YYYY/MM/DD')).toEqual(['year', 'month', 'day'])
|
||||
expect(dateFieldOrder('de-DE', 'auto')).toEqual(['day', 'month', 'year'])
|
||||
expect(dateFieldOrder('en-US')).toEqual(['month', 'day', 'year'])
|
||||
expect(dateFieldOrder('ko-KR', null)).toEqual(['year', 'month', 'day'])
|
||||
})
|
||||
|
||||
it('FE-UTIL-DATEFMT-019: dateLayout knows the five formats only, isNumericDate spots names', () => {
|
||||
expect(dateLayout('DD/MM/YYYY')).toEqual({ order: ['day', 'month', 'year'], separator: '/' })
|
||||
expect(dateLayout('auto')).toBeNull()
|
||||
expect(dateLayout('nope')).toBeNull()
|
||||
expect(dateLayout(undefined)).toBeNull()
|
||||
expect(isNumericDate()).toBe(true)
|
||||
expect(isNumericDate({ month: '2-digit' })).toBe(true)
|
||||
expect(isNumericDate({ month: 'short' })).toBe(false)
|
||||
expect(isNumericDate({ weekday: 'long' })).toBe(false)
|
||||
expect(isNumericDate({ era: 'short' })).toBe(false)
|
||||
expect(isNumericDate({ dateStyle: 'short' })).toBe(true)
|
||||
expect(isNumericDate({ dateStyle: 'long' })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('dateFormatOptions', () => {
|
||||
it('FE-UTIL-DATEFMT-020: every choice is labelled by how it writes 31 December, auto with its example', () => {
|
||||
expect(dateFormatOptions('en-US', 'Automatic')).toEqual([
|
||||
{ value: 'auto', label: 'Automatic (12/31/2026)' },
|
||||
{ value: 'YYYY-MM-DD', label: '2026-12-31' },
|
||||
{ value: 'YYYY/MM/DD', label: '2026/12/31' },
|
||||
{ value: 'DD.MM.YYYY', label: '31.12.2026' },
|
||||
{ value: 'DD/MM/YYYY', label: '31/12/2026' },
|
||||
{ value: 'MM/DD/YYYY', label: '12/31/2026' },
|
||||
])
|
||||
expect(dateFormatOptions('de-DE', 'Automatisch')[0]).toEqual({ value: 'auto', label: 'Automatisch (31.12.2026)' })
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,179 @@
|
||||
import { DATE_FORMAT_VALUES, type DateFormat } from '@trek/shared'
|
||||
|
||||
/**
|
||||
* The user's date format (#2971), applied wherever a date is written in numbers.
|
||||
*
|
||||
* Every function here takes the format as an argument and stays pure; the
|
||||
* current user's value comes from `useDateFormat()` (or `readDateFormat()`
|
||||
* outside React) in hooks/useDateFormat.ts. 'auto', an unknown value or none
|
||||
* at all leaves everything to `Intl` and the interface language, exactly as
|
||||
* TREK wrote dates before the setting existed.
|
||||
*
|
||||
* Only the numeric part follows the setting. A date written with a weekday or
|
||||
* a month name ("Tue, 3 Nov") keeps the language's own wording and order: the
|
||||
* setting answers "how do I read 03/11", not "what is November called".
|
||||
*/
|
||||
|
||||
export type DateField = 'day' | 'month' | 'year'
|
||||
|
||||
interface DateLayout {
|
||||
order: DateField[]
|
||||
separator: string
|
||||
}
|
||||
|
||||
const LAYOUTS: Record<Exclude<DateFormat, 'auto'>, DateLayout> = {
|
||||
'YYYY-MM-DD': { order: ['year', 'month', 'day'], separator: '-' },
|
||||
'YYYY/MM/DD': { order: ['year', 'month', 'day'], separator: '/' },
|
||||
'DD.MM.YYYY': { order: ['day', 'month', 'year'], separator: '.' },
|
||||
'DD/MM/YYYY': { order: ['day', 'month', 'year'], separator: '/' },
|
||||
'MM/DD/YYYY': { order: ['month', 'day', 'year'], separator: '/' },
|
||||
}
|
||||
|
||||
/** The order and separator of a chosen format; null for 'auto' or anything unknown. */
|
||||
export function dateLayout(format: string | null | undefined): DateLayout | null {
|
||||
if (!format || format === 'auto') return null
|
||||
return LAYOUTS[format as keyof typeof LAYOUTS] ?? null
|
||||
}
|
||||
|
||||
const DATE_KEYS = ['day', 'month', 'year'] as const
|
||||
const TIME_KEYS = ['hour', 'minute', 'second', 'dayPeriod', 'fractionalSecondDigits'] as const
|
||||
|
||||
/**
|
||||
* Whether these options write the date in numbers only: no weekday, no month
|
||||
* name, no era. `dateStyle: 'short'` is numeric in every language; the longer
|
||||
* styles name the month.
|
||||
*/
|
||||
export function isNumericDate(opts: Intl.DateTimeFormatOptions = {}): boolean {
|
||||
if (opts.weekday || opts.era) return false
|
||||
if (opts.dateStyle) return opts.dateStyle === 'short'
|
||||
return !opts.month || opts.month === 'numeric' || opts.month === '2-digit'
|
||||
}
|
||||
|
||||
const hasAny = (opts: Intl.DateTimeFormatOptions, keys: readonly string[]) =>
|
||||
keys.some((key) => (opts as Record<string, unknown>)[key] !== undefined)
|
||||
|
||||
/** The date in the layout's order, each field the options ask for (all three when they name none). */
|
||||
function numericDate(date: Date, opts: Intl.DateTimeFormatOptions, layout: DateLayout): string {
|
||||
const named = opts.dateStyle ? DATE_KEYS : DATE_KEYS.filter((key) => opts[key] !== undefined)
|
||||
const fields = named.length > 0 ? named : DATE_KEYS
|
||||
const parts = new Intl.DateTimeFormat('en-US', {
|
||||
timeZone: opts.timeZone,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
numberingSystem: 'latn',
|
||||
}).formatToParts(date)
|
||||
const value = (type: DateField) => parts.find((part) => part.type === type)?.value ?? ''
|
||||
const shortYear = opts.year === '2-digit'
|
||||
return layout.order
|
||||
.filter((field) => (fields as readonly string[]).includes(field))
|
||||
.map((field) => (field === 'year' && shortYear ? value('year').slice(-2) : value(field)))
|
||||
.join(layout.separator)
|
||||
}
|
||||
|
||||
/** Only the time-of-day options, for the clock part beside a numeric date. */
|
||||
function timeOptions(opts: Intl.DateTimeFormatOptions): Intl.DateTimeFormatOptions {
|
||||
const time: Intl.DateTimeFormatOptions = {}
|
||||
for (const key of [...TIME_KEYS, 'hour12', 'hourCycle', 'timeZone', 'timeZoneName'] as const) {
|
||||
const value = (opts as Record<string, unknown>)[key]
|
||||
if (value !== undefined) (time as Record<string, unknown>)[key] = value
|
||||
}
|
||||
if (opts.timeStyle) time.timeStyle = opts.timeStyle
|
||||
return time
|
||||
}
|
||||
|
||||
/**
|
||||
* `date.toLocaleDateString(locale, opts)`, with a numeric date written in the
|
||||
* chosen format. Anything with a weekday or a month name is left to `Intl`.
|
||||
*/
|
||||
export function formatDisplayDate(
|
||||
date: Date,
|
||||
locale: string | undefined,
|
||||
opts: Intl.DateTimeFormatOptions = {},
|
||||
format?: string | null
|
||||
): string {
|
||||
const layout = dateLayout(format)
|
||||
if (!layout || Number.isNaN(date.getTime())) return date.toLocaleDateString(locale, opts)
|
||||
if (opts.weekday && isNumericWithWeekday(opts)) {
|
||||
const weekday = date.toLocaleDateString(locale, { weekday: opts.weekday, timeZone: opts.timeZone })
|
||||
return `${weekday}, ${numericDate(date, opts, layout)}`
|
||||
}
|
||||
if (!isNumericDate(opts)) return date.toLocaleDateString(locale, opts)
|
||||
return numericDate(date, opts, layout)
|
||||
}
|
||||
|
||||
/** A weekday beside a date that is otherwise all numbers ("Tue, 03.11.2026"). */
|
||||
function isNumericWithWeekday(opts: Intl.DateTimeFormatOptions): boolean {
|
||||
if (opts.era || opts.dateStyle) return false
|
||||
return (opts.month === 'numeric' || opts.month === '2-digit') && opts.day !== undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* `date.toLocaleString(locale, opts)`, with a numeric date part written in the
|
||||
* chosen format and the time of day left to `Intl` (12h/24h stays the
|
||||
* caller's option). Options naming only a time are passed through untouched.
|
||||
*/
|
||||
export function formatDisplayDateTime(
|
||||
date: Date,
|
||||
locale: string | undefined,
|
||||
opts: Intl.DateTimeFormatOptions = {},
|
||||
format?: string | null
|
||||
): string {
|
||||
const layout = dateLayout(format)
|
||||
const named = hasAny(opts, DATE_KEYS) || !!opts.dateStyle
|
||||
const timed = hasAny(opts, TIME_KEYS) || !!opts.timeStyle
|
||||
if (!layout || !isNumericDate(opts) || Number.isNaN(date.getTime())) return date.toLocaleString(locale, opts)
|
||||
if (!named && timed) return date.toLocaleString(locale, opts)
|
||||
const day = numericDate(date, opts, layout)
|
||||
if (named && !timed) return day
|
||||
const time = date.toLocaleTimeString(locale, timed ? timeOptions(opts) : { timeZone: opts.timeZone })
|
||||
return `${day} ${time}`
|
||||
}
|
||||
|
||||
/**
|
||||
* A drop-in for `new Intl.DateTimeFormat(locale, opts)` where only `.format()`
|
||||
* is used: numeric dates in the chosen format, the rest by `Intl`.
|
||||
*/
|
||||
export function displayDateFormatter(
|
||||
locale: string | undefined,
|
||||
opts: Intl.DateTimeFormatOptions = {},
|
||||
format?: string | null
|
||||
): { format: (date: Date | number) => string } {
|
||||
const intl = new Intl.DateTimeFormat(locale, opts)
|
||||
const layout = dateLayout(format)
|
||||
if (!layout || !isNumericDate(opts)) return { format: (date) => intl.format(date) }
|
||||
return {
|
||||
format: (date) => {
|
||||
const value = typeof date === 'number' ? new Date(date) : date
|
||||
return hasAny(opts, TIME_KEYS) || opts.timeStyle
|
||||
? formatDisplayDateTime(value, locale, opts, format)
|
||||
: formatDisplayDate(value, locale, opts, format)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** The order the fields of a typed date are read in: the chosen format's, or the language's. */
|
||||
export function dateFieldOrder(locale: string | undefined, format?: string | null): DateField[] {
|
||||
const layout = dateLayout(format)
|
||||
if (layout) return layout.order
|
||||
return new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'numeric', year: 'numeric' })
|
||||
.formatToParts(new Date(2001, 5, 15))
|
||||
.filter((part) => part.type === 'day' || part.type === 'month' || part.type === 'year')
|
||||
.map((part) => part.type as DateField)
|
||||
}
|
||||
|
||||
/** 31 December: no day of it reads as a month, so every option's example is unambiguous. */
|
||||
const SAMPLE = new Date(Date.UTC(2026, 11, 31))
|
||||
|
||||
/**
|
||||
* The choices of the setting, each labelled by how it writes 31 December, and
|
||||
* 'auto' by `autoLabel` with the language's own way in brackets.
|
||||
*/
|
||||
export function dateFormatOptions(locale: string | undefined, autoLabel: string): { value: DateFormat; label: string }[] {
|
||||
const example = (format: DateFormat) =>
|
||||
formatDisplayDate(SAMPLE, locale, { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' }, format)
|
||||
return DATE_FORMAT_VALUES.map((value) => ({
|
||||
value,
|
||||
label: value === 'auto' ? `${autoLabel} (${example(value)})` : example(value),
|
||||
}))
|
||||
}
|
||||
@@ -15,4 +15,18 @@ describe('dateInputPattern', () => {
|
||||
expect(dateInputPattern('ar')).not.toMatch(/[٠-٩\d]/);
|
||||
expect(dateInputPattern('ar')).toContain('YYYY');
|
||||
});
|
||||
|
||||
it('writes a chosen date format (#2971) as its own pattern, whatever the language', () => {
|
||||
expect(dateInputPattern('en-US', 'YYYY-MM-DD')).toBe('YYYY-MM-DD');
|
||||
expect(dateInputPattern('en-US', 'DD.MM.YYYY')).toBe('DD.MM.YYYY');
|
||||
expect(dateInputPattern('de-DE', 'MM/DD/YYYY')).toBe('MM/DD/YYYY');
|
||||
expect(dateInputPattern('ko-KR', 'YYYY/MM/DD')).toBe('YYYY/MM/DD');
|
||||
});
|
||||
|
||||
it('keeps the locale pattern for auto, an unknown value or none', () => {
|
||||
expect(dateInputPattern('de-DE', 'auto')).toBe('DD.MM.YYYY');
|
||||
expect(dateInputPattern('en-US', 'auto')).toBe('MM/DD/YYYY');
|
||||
expect(dateInputPattern('en-GB', 'DD-MM-YY')).toBe('DD/MM/YYYY');
|
||||
expect(dateInputPattern('en-GB', null)).toBe('DD/MM/YYYY');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { dateLayout } from './dateFormat';
|
||||
|
||||
const LETTERS: Record<string, string> = { day: 'DD', month: 'MM', year: 'YYYY' };
|
||||
|
||||
/**
|
||||
@@ -5,9 +7,11 @@ const LETTERS: Record<string, string> = { day: 'DD', month: 'MM', year: 'YYYY' }
|
||||
* locale's own separators. It is the hint of the manual date field, which is
|
||||
* pre-filled and read back in this same order, so a Korean user sees
|
||||
* "YYYY. MM. DD." rather than a German "DD.MM.YYYY". Direction marks are dropped:
|
||||
* the hint shows the shape, the field itself carries the script.
|
||||
* the hint shows the shape, the field itself carries the script. A chosen date
|
||||
* format (#2971) is its own pattern.
|
||||
*/
|
||||
export function dateInputPattern(locale: string): string {
|
||||
export function dateInputPattern(locale: string, format?: string | null): string {
|
||||
if (dateLayout(format)) return String(format);
|
||||
return new Intl.DateTimeFormat(locale, { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' })
|
||||
.formatToParts(new Date(Date.UTC(2001, 5, 15)))
|
||||
.map((part) => LETTERS[part.type] ?? part.value.replace(/[]/g, ''))
|
||||
|
||||
Reference in New Issue
Block a user