Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Oct 22, 2023
1 parent 5d7b692 commit 0fa45bb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/util/date-time/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Convert to `Date` format, support `Date, number, string` types, If the argument
Format date, supports `Date, number, string` types, If the argument is a number, it is treated as a timestamp (Support seconds and milliseconds timestamp).

* Please refer to [date-fnd format](https://date-fns.org/v2.30.0/docs/format) for string format
* `dateLocale` uses `NZ_DATE_LOCALE` by default to be consistent with NG-ZORRO
* `dateLocale` Recommended to be consistent with NG-ZORRO by using `inject(NZ_DATE_LOCALE)`

## dateTimePickerUtil

Expand Down
2 changes: 1 addition & 1 deletion packages/util/date-time/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type: Tools
格式化日期,支持 `Date, number, string` 类型,如果是 `number` 表示 Unix timestamp (支持秒与毫秒)。

* 字符串格式请参考 [date-fnd format](https://date-fns.org/v2.30.0/docs/format)
* `dateLocale` 默认使用 `NZ_DATE_LOCALE` 与 NG-ZORRO 保持一致
* `dateLocale` 建议通过使用 `inject(NZ_DATE_LOCALE)` 与 NG-ZORRO 保持一致

## dateTimePickerUtil

Expand Down
8 changes: 3 additions & 5 deletions packages/util/date-time/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { inject } from '@angular/core';

import {
addDays,
endOfDay,
Expand All @@ -20,7 +18,7 @@ import {
} from 'date-fns';

import type { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NZ_DATE_LOCALE, DateLocale } from 'ng-zorro-antd/i18n';
import { DateLocale } from 'ng-zorro-antd/i18n';

/**
* Get the time range, return `[ Date, Date]` for the start and end dates
Expand Down Expand Up @@ -127,12 +125,12 @@ export function toDate(value?: Date | string | number | null, options?: string |
*
* @param value When is a number, it is treated as a timestamp (Support seconds and milliseconds timestamp).
* @param formatString Please refer to [date-fnd format](https://date-fns.org/v2.30.0/docs/format) for string format
* @param dateLocale `dateLocale` uses `NZ_DATE_LOCALE` by default to be consistent with NG-ZORRO
* @param dateLocale Recommended to be consistent with NG-ZORRO by using `inject(NZ_DATE_LOCALE)`
*/
export function formatDate(value: Date | string | number, formatString: string, dateLocale?: DateLocale): string {
value = toDate(value);
if (isNaN(value as NzSafeAny)) return '';

const langOpt = { locale: dateLocale ?? inject(NZ_DATE_LOCALE, { optional: true }) ?? undefined };
const langOpt = { locale: dateLocale };
return formatString === 'fn' ? formatDistanceToNow(value, langOpt) : format(value, formatString, langOpt);
}

0 comments on commit 0fa45bb

Please sign in to comment.