diff --git a/packages/util/date-time/index.en-US.md b/packages/util/date-time/index.en-US.md index d55dca5b6c..133135182a 100644 --- a/packages/util/date-time/index.en-US.md +++ b/packages/util/date-time/index.en-US.md @@ -6,14 +6,15 @@ type: Tools ## toDate -Convert to `Date` format, support `Date, number, string` types, If the argument is a number, it is treated as a timestamp (Support seconds and milliseconds timestamp). +Convert to `Date` format, support `Date, number, string` types, If the argument is a number, it is treated as a timestamp. * `formatString` If parsing fails try to parse the date by pressing `formatString` * `defaultValue` If parsing fails returned default value, default: `new Date(NaN)` +* `timestampSecond` Whether the incoming value is in seconds ## formatDate -Format date, supports `Date, number, string` types, If the argument is a number, it is treated as a timestamp (Support seconds and milliseconds timestamp). +Format date, supports `Date, number, string` types, If the argument is a number, it is treated as a timestamp. * Please refer to [date-fnd format](https://date-fns.org/v2.30.0/docs/format) for string format * `dateLocale` Recommended to be consistent with NG-ZORRO by using `inject(NZ_DATE_LOCALE)` diff --git a/packages/util/date-time/index.zh-CN.md b/packages/util/date-time/index.zh-CN.md index 33c24aaa56..1f6c85fd1c 100644 --- a/packages/util/date-time/index.zh-CN.md +++ b/packages/util/date-time/index.zh-CN.md @@ -6,14 +6,15 @@ type: Tools ## toDate -转换成 `Date` 格式,支持 `Date, number, string` 类型,如果是 `number` 表示 Unix timestamp (支持秒与毫秒)。 +转换成 `Date` 格式,支持 `Date, number, string` 类型,如果是 `number` 表示 Unix timestamp。 * `formatString` 如果转换失败尝试根据 `formatString` 格式来转换 * `defaultValue` 无效日期应返回的默认值,默认:`new Date(NaN)` +* `timestampSecond` 传入值是否秒级 ## formatDate -格式化日期,支持 `Date, number, string` 类型,如果是 `number` 表示 Unix timestamp (支持秒与毫秒)。 +格式化日期,支持 `Date, number, string` 类型,如果是 `number` 表示 Unix timestamp)。 * 字符串格式请参考 [date-fnd format](https://date-fns.org/v2.30.0/docs/format) * `dateLocale` 建议通过使用 `inject(NZ_DATE_LOCALE)` 与 NG-ZORRO 保持一致 diff --git a/packages/util/date-time/time.spec.ts b/packages/util/date-time/time.spec.ts index 410278edc4..cffda8b3c3 100644 --- a/packages/util/date-time/time.spec.ts +++ b/packages/util/date-time/time.spec.ts @@ -68,10 +68,9 @@ describe('util: time', () => { expect(toDate(null).toString()).toBe(`Invalid Date`); expect(f(toDate(NOW))).toBe(`2000-01-01 00:00:00`); expect(f(toDate(+NOW))).toBe(`2000-01-01 00:00:00`); + expect(f(toDate(Math.trunc(+NOW / 1000), { timestampSecond: true }))).toBe(`2000-01-01 00:00:00`); expect(f(toDate(`${+NOW}`))).toBe(`2000-01-01 00:00:00`); expect(f(toDate(f(NOW)))).toBe(`2000-01-01 00:00:00`); - expect(f(toDate(1697950947))).toBe(`2023-10-22 13:02:27`); - expect(f(toDate(1697950947123), 'yyyy-MM-dd HH:mm:ss.SSS')).toBe(`2023-10-22 13:02:27.123`); expect(isNaN(toDate(new String('') as NzSafeAny) as NzSafeAny)).toBe(true); }); diff --git a/packages/util/date-time/time.ts b/packages/util/date-time/time.ts index 7ad6f75bec..726f25c2d4 100644 --- a/packages/util/date-time/time.ts +++ b/packages/util/date-time/time.ts @@ -89,17 +89,19 @@ export interface ToDateOptions { formatString?: string; /** If parsing fails returned default value, default: `new Date(NaN)` */ defaultValue?: NzSafeAny; + timestampSecond?: boolean; } /** * Convert to `Date` format * - * @param value When is a number, it is treated as a timestamp (Support seconds and milliseconds timestamp). + * @param value When is a number, it's treated as a timestamp; If it's seconds, you need to provide the `options.timestampSecond` parameter. */ export function toDate(value?: Date | string | number | null, options?: string | ToDateOptions): Date { - const { formatString, defaultValue } = { + const { formatString, defaultValue, timestampSecond } = { formatString: 'yyyy-MM-dd HH:mm:ss', defaultValue: new Date(NaN), + timestampSecond: false, ...(typeof options === 'string' ? { formatString: options } : options) }; if (value == null) { @@ -108,9 +110,9 @@ export function toDate(value?: Date | string | number | null, options?: string | if (value instanceof Date) { return value; } - if (typeof value === 'number' || (typeof value === 'string' && /[0-9]{10,13}/.test(value))) { + if (typeof value === 'number' || (typeof value === 'string' && /^[0-9]+$/.test(value))) { const valueNumber = +value; - return new Date(`${value}`.length === 10 ? valueNumber * 1000 : valueNumber); + return new Date(timestampSecond ? valueNumber * 1000 : valueNumber); } let tryDate = parseISO(value); if (isNaN(tryDate as NzSafeAny)) {