Skip to content

Commit

Permalink
fix(i18n): formatIsoDate handles trailing 0s (#1821)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewwylde authored Nov 27, 2024
1 parent dfeef33 commit 58af65d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/core/i18n/src/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ describe('i18n', () => {
const decimalTimestamp = formatIsoDate('2019-10-03T14:13:15.000Z')
const integerTimestamp = formatIsoDate('2019-10-03T14:13:15.000Z')

expect(formatIsoDate('2019-10-03T14:13:15.123Z')).toBe('Oct 3, 2019, 2:13 PM')
expect(formatIsoDate('2019-10-03T14:13:15.000Z')).toBe('Oct 3, 2019, 2:13 PM')
expect(formatIsoDate('2019-10-03T14:13:15Z')).toBe('Oct 3, 2019, 2:13 PM')
expect(formatIsoDate('2019-05-16T11:42:59+00:00')).toBe('May 16, 2019, 11:42 AM')
expect(formatIsoDate('2019-05-16T11:42:59-04:00')).toBe('May 16, 2019, 3:42 PM')
expect(formatIsoDate('2019-05-16T11:42:59+08:00')).toBe('May 16, 2019, 3:42 AM')

expect(formattedDateAM).toBe('May 16, 2019, 11:42 AM')
expect(formattedDatePM).toBe('May 17, 2019, 1:39 AM')
expect(january.substring(0, 7)).toBe('Jan 14,')
Expand Down
4 changes: 3 additions & 1 deletion packages/core/i18n/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export const createI18n = <MessageSource extends Record<string, any>>
const formatIsoDate = (isoDate: string): string => {
const date = Date.parse(isoDate) / 1000

return formatUnixTimeStamp(date)
// excludes milliseconds with trailing 0s
const flooredDate = Math.floor(date)
return formatUnixTimeStamp(flooredDate)
}

const t = (translationKey: PathToDotNotation<MessageSource, string>, values?: Record<string, MessageFormatPrimitiveValue> | undefined, opts?: IntlMessageFormatOptions): string => {
Expand Down

0 comments on commit 58af65d

Please sign in to comment.