Created
December 10, 2025 19:20
-
-
Save danestves/fa832ce2cc2c20211e6f9f99a91ac062 to your computer and use it in GitHub Desktop.
Format date and time using Temporal API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type DateInput = Date | string | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime | Temporal.Instant | |
| function parseToTemporalDate(input: DateInput): Temporal.PlainDate { | |
| if (input instanceof Temporal.PlainDate) { | |
| return input | |
| } | |
| if (input instanceof Temporal.PlainDateTime) { | |
| return input.toPlainDate() | |
| } | |
| if (input instanceof Temporal.ZonedDateTime) { | |
| return input.toPlainDate() | |
| } | |
| if (input instanceof Temporal.Instant) { | |
| const zonedDateTime = input.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainDate() | |
| } | |
| if (input instanceof Date) { | |
| const isoString = input.toISOString() | |
| const dateOnly = isoString.split('T')[0]! | |
| return Temporal.PlainDate.from(dateOnly) | |
| } | |
| if (typeof input === 'string') { | |
| if (input.includes('T')) { | |
| try { | |
| const instant = Temporal.Instant.from(input) | |
| const zonedDateTime = instant.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainDate() | |
| } catch { | |
| const plainDateTime = Temporal.PlainDateTime.from(input) | |
| return plainDateTime.toPlainDate() | |
| } | |
| } | |
| return Temporal.PlainDate.from(input) | |
| } | |
| throw new Error(`Unsupported date input type: ${typeof input}`) | |
| } | |
| function parseToTemporalDateTime(input: DateInput): Temporal.PlainDateTime { | |
| if (input instanceof Temporal.PlainDateTime) { | |
| return input | |
| } | |
| if (input instanceof Temporal.PlainDate) { | |
| return input.toPlainDateTime({ hour: 0, minute: 0, second: 0 }) | |
| } | |
| if (input instanceof Temporal.ZonedDateTime) { | |
| return input.toPlainDateTime() | |
| } | |
| if (input instanceof Temporal.Instant) { | |
| const zonedDateTime = input.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainDateTime() | |
| } | |
| if (input instanceof Date) { | |
| const isoString = input.toISOString() | |
| try { | |
| const instant = Temporal.Instant.from(isoString) | |
| const zonedDateTime = instant.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainDateTime() | |
| } catch { | |
| return Temporal.PlainDateTime.from(isoString.split('T')[0] + 'T00:00:00') | |
| } | |
| } | |
| if (typeof input === 'string') { | |
| if (input.includes('T')) { | |
| try { | |
| const instant = Temporal.Instant.from(input) | |
| const zonedDateTime = instant.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainDateTime() | |
| } catch { | |
| return Temporal.PlainDateTime.from(input) | |
| } | |
| } | |
| const date = Temporal.PlainDate.from(input) | |
| return date.toPlainDateTime({ hour: 0, minute: 0, second: 0 }) | |
| } | |
| throw new Error(`Unsupported date input type: ${typeof input}`) | |
| } | |
| function parseToTemporalTime(input: DateInput): Temporal.PlainTime { | |
| if (input instanceof Temporal.PlainTime) { | |
| return input | |
| } | |
| if (input instanceof Temporal.PlainDateTime) { | |
| return input.toPlainTime() | |
| } | |
| if (input instanceof Temporal.ZonedDateTime) { | |
| return input.toPlainTime() | |
| } | |
| if (input instanceof Temporal.Instant) { | |
| const zonedDateTime = input.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainTime() | |
| } | |
| if (input instanceof Date) { | |
| const isoString = input.toISOString() | |
| const instant = Temporal.Instant.from(isoString) | |
| const zonedDateTime = instant.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainTime() | |
| } | |
| if (typeof input === 'string') { | |
| if (input.includes('T')) { | |
| try { | |
| const instant = Temporal.Instant.from(input) | |
| const zonedDateTime = instant.toZonedDateTimeISO('UTC') | |
| return zonedDateTime.toPlainTime() | |
| } catch { | |
| const plainDateTime = Temporal.PlainDateTime.from(input) | |
| return plainDateTime.toPlainTime() | |
| } | |
| } | |
| throw new Error('Time string must include time component (T)') | |
| } | |
| throw new Error(`Unsupported date input type: ${typeof input}`) | |
| } | |
| export function formatDate(date: DateInput, options?: Intl.DateTimeFormatOptions, locale = 'en-US'): string { | |
| const plainDate = parseToTemporalDate(date) | |
| const defaultOptions: Intl.DateTimeFormatOptions = { | |
| month: 'short', | |
| day: '2-digit', | |
| year: 'numeric', | |
| } | |
| return plainDate.toLocaleString(locale, options ?? defaultOptions) | |
| } | |
| export function formatDateTime(date: DateInput, options?: Intl.DateTimeFormatOptions, locale = 'en-US'): string { | |
| const plainDateTime = parseToTemporalDateTime(date) | |
| const defaultOptions: Intl.DateTimeFormatOptions = { | |
| month: 'short', | |
| day: '2-digit', | |
| year: 'numeric', | |
| hour: '2-digit', | |
| minute: '2-digit', | |
| } | |
| return plainDateTime.toLocaleString(locale, options ?? defaultOptions) | |
| } | |
| export function formatTime(date: DateInput, options?: Intl.DateTimeFormatOptions, locale = 'en-US'): string { | |
| const plainTime = parseToTemporalTime(date) | |
| const defaultOptions: Intl.DateTimeFormatOptions = { | |
| hour: '2-digit', | |
| minute: '2-digit', | |
| } | |
| return plainTime.toLocaleString(locale, options ?? defaultOptions) | |
| } | |
| export function isThisYear(date: DateInput): boolean { | |
| const plainDate = parseToTemporalDate(date) | |
| const now = Temporal.Now.plainDateISO() | |
| return plainDate.year === now.year | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment