Automerge backed Markdown editor for the Atmosphere
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344import { describe, expect, it } from 'vitest'import { formatDate, formatTime } from '../datetime-format'
describe('datetime-format', () => { it('returns an empty string for an empty format', () => { const date = new Date(2026, 2, 15, 0, 5, 9) expect(formatDate(date, '')).toBe('') expect(formatTime(date, '')).toBe('') })
it('formats dates and times with shared helpers', () => { const date = new Date(2026, 2, 15, 14, 5, 9)
expect(formatDate(date, 'YYYY-MM-DD')).toBe('2026-03-15') expect(formatDate(date, 'dddd, MMMM D, YYYY')).toBe('Sunday, March 15, 2026') expect(formatTime(date, 'HH:mm')).toBe('14:05') expect(formatTime(date, 'h:mm A')).toBe('2:05 PM') })
it('formats every date token without token-order collisions', () => { const date = new Date(2026, 0, 5, 0, 5, 9) expect(formatDate(date, 'YYYY YY MMMM MMM MM M dddd ddd DD D')).toBe( '2026 26 January Jan 01 1 Monday Mon 05 5', ) })
it.each([ [1, '1st'], [2, '2nd'], [3, '3rd'], [4, '4th'], [11, '11th'], [12, '12th'], [13, '13th'], [21, '21st'], [22, '22nd'], [23, '23rd'], [30, '30th'], [31, '31st'], ])('formats ordinal day %s as %s', (day, ordinal) => { const date = new Date(2025, 0, day) expect(formatDate(date, 'MMMM Do, YYYY')).toBe(`January ${ordinal}, 2025`) expect(formatDate(date, 'Do D DD')).toBe(`${ordinal} ${day} ${String(day).padStart(2, '0')}`) })
it('formats both twelve-hour periods and every time token', () => { const morning = new Date(2026, 0, 5, 0, 5, 9) const evening = new Date(2026, 0, 5, 23, 15, 42) expect(formatTime(morning, 'HH H hh h mm m ss s A a')).toBe('00 0 12 12 05 5 09 9 AM am') expect(formatTime(evening, 'HH H hh h mm m ss s A a')).toBe('23 23 11 11 15 15 42 42 PM pm') })})