Skip to content

Commit

Permalink
chore: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Oct 22, 2023
1 parent 0fa45bb commit d40814f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/util/date-time/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
5 changes: 3 additions & 2 deletions packages/util/date-time/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 保持一致
Expand Down
3 changes: 1 addition & 2 deletions packages/util/date-time/time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
10 changes: 6 additions & 4 deletions packages/util/date-time/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down

0 comments on commit d40814f

Please sign in to comment.