From 2054c10fcd9482b7ba0c1a7a5735021875e482fb Mon Sep 17 00:00:00 2001 From: danyue <37496285+heihei12305@users.noreply.github.com> Date: Wed, 14 Sep 2022 23:07:26 +0800 Subject: [PATCH] fix(next/antd): fix moment timestamp (#3395) * fix(next): fix moment timestamp * feat(next\antd): update moment timestamp test * feat(next\antd): add moment timestamp test * feat(next\antd): add moment timestamp array format test --- packages/antd/__tests__/moment.spec.ts | 6 ++++++ packages/antd/src/__builtins__/moment.ts | 6 ++++++ packages/next/__tests__/moment.spec.ts | 6 ++++++ packages/next/src/__builtins__/moment.ts | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/packages/antd/__tests__/moment.spec.ts b/packages/antd/__tests__/moment.spec.ts index a42d79f93a9..2e1ad350237 100644 --- a/packages/antd/__tests__/moment.spec.ts +++ b/packages/antd/__tests__/moment.spec.ts @@ -23,6 +23,12 @@ test('formatMomentValue is usable', () => { ) expect(formatMomentValue('12:11', 'HH:mm')).toBe('12:11') expect(formatMomentValue('12:11:11', 'HH:mm:ss')).toBe('12:11:11') + expect(formatMomentValue(1663155911097, 'YYYY-MM-DD HH:mm:ss')).toBe( + moment(1663155911097).format('YYYY-MM-DD HH:mm:ss') + ) + expect(formatMomentValue([1663155911097], ['YYYY-MM-DD HH:mm:ss'])).toEqual([ + moment(1663155911097).format('YYYY-MM-DD HH:mm:ss'), + ]) expect( formatMomentValue( ['2021-12-21 15:47:00', '2021-12-29 15:47:00'], diff --git a/packages/antd/src/__builtins__/moment.ts b/packages/antd/src/__builtins__/moment.ts index 5c9f50364ad..6c76b959d93 100644 --- a/packages/antd/src/__builtins__/moment.ts +++ b/packages/antd/src/__builtins__/moment.ts @@ -24,6 +24,9 @@ export const formatMomentValue = ( if (isEmpty(_format)) { return date } + if (typeof date === 'number') { + return moment(date).format(_format) + } return moment(date, _format).format(_format) } else { if (isFn(format)) { @@ -32,6 +35,9 @@ export const formatMomentValue = ( if (isEmpty(format)) { return date } + if (typeof date === 'number') { + return moment(date).format(format) + } return moment(date, format).format(format) } } diff --git a/packages/next/__tests__/moment.spec.ts b/packages/next/__tests__/moment.spec.ts index 5d02cb44dc3..d4c857a0cb1 100644 --- a/packages/next/__tests__/moment.spec.ts +++ b/packages/next/__tests__/moment.spec.ts @@ -23,6 +23,12 @@ test('formatMomentValue is usable', () => { ) expect(formatMomentValue('12:11', 'HH:mm')).toBe('12:11') expect(formatMomentValue('12:11:11', 'HH:mm:ss')).toBe('12:11:11') + expect(formatMomentValue(1663155911097, 'YYYY-MM-DD HH:mm:ss')).toBe( + moment(1663155911097).format('YYYY-MM-DD HH:mm:ss') + ) + expect(formatMomentValue([1663155911097], ['YYYY-MM-DD HH:mm:ss'])).toEqual([ + moment(1663155911097).format('YYYY-MM-DD HH:mm:ss'), + ]) expect( formatMomentValue( ['2021-12-21 15:47:00', '2021-12-29 15:47:00'], diff --git a/packages/next/src/__builtins__/moment.ts b/packages/next/src/__builtins__/moment.ts index 2b6f088cdef..23a9953c238 100644 --- a/packages/next/src/__builtins__/moment.ts +++ b/packages/next/src/__builtins__/moment.ts @@ -28,6 +28,9 @@ export const formatMomentValue = ( if (isEmpty(_format)) { return date } + if (typeof date === 'number') { + return moment(date).format(_format) + } return moment(date, _format).format(_format) } else { if (isFn(format)) { @@ -36,6 +39,9 @@ export const formatMomentValue = ( if (isEmpty(format)) { return date } + if (typeof date === 'number') { + return moment(date).format(format) + } return moment(date, format).format(format) } }