DatePicker日期选择框

输入或选择日期的控件。

何时使用

当用户需要输入一个日期,可以点击标准输入框,弹出日期面板进行选择。

  1. import { NzDatePickerModule } from 'ng-zorro-antd/date-picker';

代码演示

DatePicker日期选择框 - 图1

基本

最简单的用法,在浮层中可以选择或者输入日期。

  1. import { Component } from '@angular/core';
  2. import getISOWeek from 'date-fns/getISOWeek';
  3. import { en_US, NzI18nService, zh_CN } from 'ng-zorro-antd/i18n';
  4. @Component({
  5. selector: 'nz-demo-date-picker-basic',
  6. template: `
  7. <nz-date-picker [(ngModel)]="date" (ngModelChange)="onChange($event)"></nz-date-picker>
  8. <br />
  9. <nz-date-picker nzMode="week" [(ngModel)]="date" (ngModelChange)="getWeek($event)"></nz-date-picker>
  10. <br />
  11. <nz-date-picker nzMode="month" [(ngModel)]="date" (ngModelChange)="onChange($event)"></nz-date-picker>
  12. <br />
  13. <nz-date-picker nzMode="year" [(ngModel)]="date" (ngModelChange)="onChange($event)"></nz-date-picker>
  14. <br />
  15. <button nz-button nzType="default" (click)="changeLanguage()">Switch language for all pickers</button>
  16. `,
  17. styles: [
  18. `
  19. nz-date-picker {
  20. margin: 0 8px 12px 0;
  21. }
  22. `
  23. ]
  24. })
  25. export class NzDemoDatePickerBasicComponent {
  26. date = null;
  27. isEnglish = false;
  28. constructor(private i18n: NzI18nService) {}
  29. onChange(result: Date): void {
  30. console.log('onChange: ', result);
  31. }
  32. getWeek(result: Date): void {
  33. console.log('week: ', getISOWeek(result));
  34. }
  35. changeLanguage(): void {
  36. this.i18n.setLocale(this.isEnglish ? zh_CN : en_US);
  37. this.isEnglish = !this.isEnglish;
  38. }
  39. }

DatePicker日期选择框 - 图2

日期格式

使用 nzFormat 属性,可以自定义日期显示格式。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-date-picker-format',
  4. template: `
  5. <nz-date-picker [nzFormat]="dateFormat"></nz-date-picker>
  6. <br />
  7. <nz-date-picker nzMode="month" [nzFormat]="monthFormat"></nz-date-picker>
  8. <br />
  9. <nz-range-picker [nzFormat]="dateFormat"></nz-range-picker>
  10. `,
  11. styles: [
  12. `
  13. nz-date-picker,
  14. nz-range-picker {
  15. margin: 0 8px 12px 0;
  16. }
  17. `
  18. ]
  19. })
  20. export class NzDemoDatePickerFormatComponent {
  21. dateFormat = 'yyyy/MM/dd';
  22. monthFormat = 'yyyy/MM';
  23. }

DatePicker日期选择框 - 图3

日期时间选择

增加选择时间功能,当 nzShowTime 为一个对象时,其属性会传递给内建的 TimePicker

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-date-picker-time',
  4. template: `
  5. <nz-date-picker
  6. nzShowTime
  7. nzFormat="yyyy-MM-dd HH:mm:ss"
  8. ngModel
  9. (ngModelChange)="onChange($event)"
  10. (nzOnOk)="onOk($event)"
  11. ></nz-date-picker>
  12. <br />
  13. <nz-range-picker
  14. [nzShowTime]="{ nzFormat: 'HH:mm' }"
  15. nzFormat="yyyy-MM-dd HH:mm"
  16. ngModel
  17. (ngModelChange)="onChange($event)"
  18. (nzOnCalendarChange)="onCalendarChange($event)"
  19. (nzOnOk)="onOk($event)"
  20. ></nz-range-picker>
  21. `,
  22. styles: [
  23. `
  24. nz-date-picker,
  25. nz-range-picker {
  26. margin: 0 8px 12px 0;
  27. }
  28. `
  29. ]
  30. })
  31. export class NzDemoDatePickerTimeComponent {
  32. onChange(result: Date): void {
  33. console.log('Selected Time: ', result);
  34. }
  35. onOk(result: Date | Date[] | null): void {
  36. console.log('onOk', result);
  37. }
  38. onCalendarChange(result: Array<Date | null>): void {
  39. console.log('onCalendarChange', result);
  40. }
  41. }

DatePicker日期选择框 - 图4

不可选择日期和时间

可用 nzDisabledDatenzDisabledTime 分别禁止选择部分日期和时间。

  1. import { Component } from '@angular/core';
  2. import differenceInCalendarDays from 'date-fns/differenceInCalendarDays';
  3. import setHours from 'date-fns/setHours';
  4. import { DisabledTimeFn, DisabledTimePartial } from 'ng-zorro-antd/date-picker';
  5. @Component({
  6. selector: 'nz-demo-date-picker-disabled-date',
  7. template: `
  8. <nz-date-picker
  9. nzFormat="yyyy-MM-dd HH:mm:ss"
  10. [nzDisabledDate]="disabledDate"
  11. [nzDisabledTime]="disabledDateTime"
  12. [nzShowTime]="{ nzDefaultOpenValue: timeDefaultValue }"
  13. ></nz-date-picker>
  14. <br />
  15. <nz-date-picker nzMode="month" [nzDisabledDate]="disabledDate"></nz-date-picker>
  16. <br />
  17. <nz-date-picker nzMode="year" [nzDisabledDate]="disabledDate"></nz-date-picker>
  18. <br />
  19. <nz-range-picker
  20. [nzDisabledDate]="disabledDate"
  21. [nzDisabledTime]="disabledRangeTime"
  22. [nzShowTime]="{ nzHideDisabledOptions: true, nzDefaultOpenValue: timeDefaultValue }"
  23. nzFormat="yyyy-MM-dd HH:mm:ss"
  24. ></nz-range-picker>
  25. `,
  26. styles: [
  27. `
  28. nz-date-picker,
  29. nz-range-picker {
  30. margin: 0 8px 12px 0;
  31. }
  32. `
  33. ]
  34. })
  35. export class NzDemoDatePickerDisabledDateComponent {
  36. today = new Date();
  37. timeDefaultValue = setHours(new Date(), 0);
  38. range(start: number, end: number): number[] {
  39. const result: number[] = [];
  40. for (let i = start; i < end; i++) {
  41. result.push(i);
  42. }
  43. return result;
  44. }
  45. disabledDate = (current: Date): boolean => {
  46. // Can not select days before today and today
  47. return differenceInCalendarDays(current, this.today) > 0;
  48. };
  49. disabledDateTime: DisabledTimeFn = () => {
  50. return {
  51. nzDisabledHours: () => this.range(0, 24).splice(4, 20),
  52. nzDisabledMinutes: () => this.range(30, 60),
  53. nzDisabledSeconds: () => [55, 56]
  54. };
  55. };
  56. disabledRangeTime: DisabledTimeFn = (_value, type?: DisabledTimePartial) => {
  57. if (type === 'start') {
  58. return {
  59. nzDisabledHours: () => this.range(0, 60).splice(4, 20),
  60. nzDisabledMinutes: () => this.range(30, 60),
  61. nzDisabledSeconds: () => [55, 56]
  62. };
  63. }
  64. return {
  65. nzDisabledHours: () => this.range(0, 60).splice(20, 4),
  66. nzDisabledMinutes: () => this.range(0, 31),
  67. nzDisabledSeconds: () => [55, 56]
  68. };
  69. };
  70. }

DatePicker日期选择框 - 图5

预设范围

RangePicker 可以设置常用的 预设范围 提高用户体验。

  1. import { Component } from '@angular/core';
  2. import endOfMonth from 'date-fns/endOfMonth';
  3. @Component({
  4. selector: 'nz-demo-date-picker-presetted-ranges',
  5. template: `
  6. <nz-range-picker [nzRanges]="ranges" ngModel (ngModelChange)="onChange($event)"></nz-range-picker>
  7. <br />
  8. <nz-range-picker
  9. [nzRanges]="ranges"
  10. nzShowTime
  11. nzFormat="yyyy/MM/dd HH:mm:ss"
  12. ngModel
  13. (ngModelChange)="onChange($event)"
  14. ></nz-range-picker>
  15. `,
  16. styles: [
  17. `
  18. nz-date-picker,
  19. nz-range-picker {
  20. margin: 0 8px 12px 0;
  21. }
  22. `
  23. ]
  24. })
  25. export class NzDemoDatePickerPresettedRangesComponent {
  26. ranges = { Today: [new Date(), new Date()], 'This Month': [new Date(), endOfMonth(new Date())] };
  27. onChange(result: Date[]): void {
  28. console.log('From: ', result[0], ', to: ', result[1]);
  29. }
  30. }

DatePicker日期选择框 - 图6

定制日期单元格

使用 nzDateRender 可以自定义日期单元格的内容和样式。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-date-picker-date-render',
  4. template: `
  5. <nz-date-picker [nzDateRender]="tplRender"></nz-date-picker>
  6. <nz-range-picker [nzDateRender]="tplRender"></nz-range-picker>
  7. <ng-template #tplRender let-current>
  8. <div class="ant-picker-cell-inner" [class.border]="current.getDate() === 1">
  9. {{ current.getDate() }}
  10. </div>
  11. </ng-template>
  12. `,
  13. styles: [
  14. `
  15. nz-date-picker,
  16. nz-range-picker {
  17. margin: 0 8px 12px 0;
  18. }
  19. .border {
  20. border: 1px solid #1890ff;
  21. border-radius: 50%;
  22. }
  23. `
  24. ]
  25. })
  26. export class NzDemoDatePickerDateRenderComponent {}

DatePicker日期选择框 - 图7

范围选择器

通过设置 nzMode 属性,指定范围选择器类型。

  1. import { Component } from '@angular/core';
  2. import getISOWeek from 'date-fns/getISOWeek';
  3. @Component({
  4. selector: 'nz-demo-date-picker-range-picker',
  5. template: `
  6. <nz-range-picker [(ngModel)]="date" (ngModelChange)="onChange($event)"></nz-range-picker>
  7. <br />
  8. <nz-range-picker [nzShowTime]="true" [(ngModel)]="date" (ngModelChange)="onChange($event)"></nz-range-picker>
  9. <br />
  10. <nz-range-picker nzMode="week" [(ngModel)]="date" (ngModelChange)="getWeek($event)"></nz-range-picker>
  11. <br />
  12. <nz-range-picker nzMode="month" [(ngModel)]="date" (ngModelChange)="onChange($event)"></nz-range-picker>
  13. <br />
  14. <nz-range-picker nzMode="year" [(ngModel)]="date" (ngModelChange)="onChange($event)"></nz-range-picker>
  15. `,
  16. styles: [
  17. `
  18. nz-range-picker {
  19. margin: 0 8px 12px 0;
  20. }
  21. `
  22. ]
  23. })
  24. export class NzDemoDatePickerRangePickerComponent {
  25. date = null;
  26. onChange(result: Date[]): void {
  27. console.log('onChange: ', result);
  28. }
  29. getWeek(result: Date[]): void {
  30. console.log('week: ', result.map(getISOWeek));
  31. }
  32. }

DatePicker日期选择框 - 图8

三种大小

三种大小的输入框,若不设置,则为 default

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-date-picker-size',
  4. template: `
  5. <nz-radio-group [(ngModel)]="size">
  6. <label nz-radio-button nzValue="large">large</label>
  7. <label nz-radio-button nzValue="default">default</label>
  8. <label nz-radio-button nzValue="small">small</label>
  9. </nz-radio-group>
  10. <br />
  11. <br />
  12. <nz-date-picker [nzSize]="size"></nz-date-picker>
  13. <br />
  14. <nz-date-picker nzMode="week" [nzSize]="size"></nz-date-picker>
  15. <br />
  16. <nz-date-picker nzMode="month" [nzSize]="size"></nz-date-picker>
  17. <br />
  18. <nz-range-picker [nzSize]="size"></nz-range-picker>
  19. `,
  20. styles: [
  21. `
  22. nz-date-picker,
  23. nz-range-picker {
  24. margin: 0 8px 12px 0;
  25. }
  26. `
  27. ]
  28. })
  29. export class NzDemoDatePickerSizeComponent {
  30. size: 'large' | 'small' | 'default' = 'default';
  31. }

DatePicker日期选择框 - 图9

禁用

选择框的不可用状态。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-date-picker-disabled',
  4. template: `
  5. <nz-date-picker nzDisabled></nz-date-picker>
  6. <br />
  7. <nz-date-picker nzMode="month" nzDisabled></nz-date-picker>
  8. <br />
  9. <nz-range-picker nzDisabled></nz-range-picker>
  10. `,
  11. styles: [
  12. `
  13. nz-date-picker,
  14. nz-range-picker {
  15. margin: 0 8px 12px 0;
  16. }
  17. `
  18. ]
  19. })
  20. export class NzDemoDatePickerDisabledComponent {}

DatePicker日期选择框 - 图10

自定义日期范围选择

RangePicker 无法满足业务需求时,可以使用两个 DatePicker 实现类似的功能。

  • 通过设置 nzDisabledDate 方法,来约束开始和结束日期。
  • 通过 open() 来优化交互。
  1. import { Component, ViewChild } from '@angular/core';
  2. import { NzDatePickerComponent } from 'ng-zorro-antd/date-picker';
  3. @Component({
  4. selector: 'nz-demo-date-picker-start-end',
  5. template: `
  6. <nz-date-picker
  7. [nzDisabledDate]="disabledStartDate"
  8. nzShowTime
  9. nzFormat="yyyy-MM-dd HH:mm:ss"
  10. [(ngModel)]="startValue"
  11. nzPlaceHolder="Start"
  12. (nzOnOpenChange)="handleStartOpenChange($event)"
  13. ></nz-date-picker>
  14. <nz-date-picker
  15. #endDatePicker
  16. [nzDisabledDate]="disabledEndDate"
  17. nzShowTime
  18. nzFormat="yyyy-MM-dd HH:mm:ss"
  19. [(ngModel)]="endValue"
  20. nzPlaceHolder="End"
  21. (nzOnOpenChange)="handleEndOpenChange($event)"
  22. ></nz-date-picker>
  23. `,
  24. styles: [
  25. `
  26. nz-date-picker {
  27. margin: 0 8px 12px 0;
  28. }
  29. `
  30. ]
  31. })
  32. export class NzDemoDatePickerStartEndComponent {
  33. startValue: Date | null = null;
  34. endValue: Date | null = null;
  35. @ViewChild('endDatePicker') endDatePicker!: NzDatePickerComponent;
  36. disabledStartDate = (startValue: Date): boolean => {
  37. if (!startValue || !this.endValue) {
  38. return false;
  39. }
  40. return startValue.getTime() > this.endValue.getTime();
  41. };
  42. disabledEndDate = (endValue: Date): boolean => {
  43. if (!endValue || !this.startValue) {
  44. return false;
  45. }
  46. return endValue.getTime() <= this.startValue.getTime();
  47. };
  48. handleStartOpenChange(open: boolean): void {
  49. if (!open) {
  50. this.endDatePicker.open();
  51. }
  52. console.log('handleStartOpenChange', open);
  53. }
  54. handleEndOpenChange(open: boolean): void {
  55. console.log('handleEndOpenChange', open);
  56. }
  57. }

DatePicker日期选择框 - 图11

额外的页脚

在浮层中加入额外的页脚,以满足某些定制信息的需求。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-date-picker-extra-footer',
  4. template: `
  5. <nz-date-picker [nzRenderExtraFooter]="footerRender"></nz-date-picker>
  6. <br />
  7. <nz-date-picker [nzRenderExtraFooter]="plainFooter" nzShowTime></nz-date-picker>
  8. <nz-range-picker [nzRenderExtraFooter]="footerRender"></nz-range-picker>
  9. <nz-range-picker [nzRenderExtraFooter]="plainFooter" nzShowTime></nz-range-picker>
  10. <nz-date-picker nzMode="month" [nzRenderExtraFooter]="footerRender"></nz-date-picker>
  11. `,
  12. styles: [
  13. `
  14. nz-date-picker,
  15. nz-range-picker {
  16. margin: 0 8px 12px 0;
  17. }
  18. `
  19. ]
  20. })
  21. export class NzDemoDatePickerExtraFooterComponent {
  22. plainFooter = 'plain extra footer';
  23. footerRender = () => 'extra footer';
  24. }

API

注意:nz-date-picker 的部分 locale 来自于 Angular 自身的国际化支持,需要在 app.module.ts 文件中 引入相应的 Angular 语言包。 例如:

  1. import { registerLocaleData } from '@angular/common';
  2. import zh from '@angular/common/locales/zh';
  3. registerLocaleData(zh);

注意: 所有输入输出日期对象均为 Date,你可以通过 date-fns 工具库获得你需要的数据。

共同的 API

以下 API 为 nz-date-picker、nz-range-picker 共享的 API。

参数说明类型默认值全局配置
[nzAllowClear]是否显示清除按钮booleantrue-
[nzAutoFocus]自动获取焦点booleanfalse-
[nzDefaultPickerValue]默认面板日期Date | Date[]--
[nzDisabled]禁用booleanfalse-
[nzDisabledDate]不可选择的日期(current: Date) => boolean--
[nzDropdownClassName]额外的弹出日历 classNamestring--
[nzFormat]展示的日期格式,见nzFormat特别说明string-
[nzInputReadOnly]为 input 标签设置只读属性(避免在移动设备上触发小键盘)booleanfalse-
[nzLocale]国际化配置object默认配置-
[nzMode]选择模式‘date’ | ‘week’ | ‘month’ | ‘year’‘date’
[nzPlaceHolder]输入框提示文字string | ‘’-
[nzPopupStyle]额外的弹出日历样式object{}-
[nzRenderExtraFooter]在面板中添加额外的页脚TemplateRef | string | (() => TemplateRef | string)-
[nzSize]输入框大小,large 高度为 40px,small 为 24px,默认是 32px‘large’ | ‘small’--
[nzSuffixIcon]自定义的后缀图标string | TemplateRef-
(nzOnOpenChange)弹出日历和关闭日历的回调EventEmitter<boolean>--

共同的方法

名称描述
open()打开日历弹层
close()关闭日历弹层

nz-date-pickercomponent

参数说明类型默认值
[(ngModel)]日期Date-

nz-date-picker[nzMode=”date”]component

参数说明类型默认值
[nzDateRender]自定义日期单元格的内容(month-picker/year-picker不支持)TemplateRef<Date> | string | ((d: Date) => TemplateRef<Date> | string)--
[nzDisabledTime]不可选择的时间(current: Date) => { nzDisabledHours, nzDisabledMinutes, nzDisabledSeconds }-
[nzShowTime]增加时间选择功能object | booleanTimePicker Options
[nzShowToday]是否展示“今天”按钮booleantrue
(nzOnOk)点击确定按钮的回调EventEmitter<Date>-

nz-range-pickercomponent

参数说明类型默认值
[(ngModel)]日期Date[]-
[nzRanges]预设时间范围快捷选择{ [ key: string ]: Date[] } | { [ key: string ]: () => Date[] }-
[nzSeparator]分隔符string‘~’
(nzOnCalendarChange)待选日期发生变化的回调EventEmitter<Date[]>-

nz-range-picker[nzMode=”date”]component

参数说明类型默认值
[nzShowTime]增加时间选择功能object | booleanTimePicker Options
[nzDisabledTime]不可选择的时间(current: Date, partial: ‘start’ | ‘end’) => { nzDisabledHours, nzDisabledMinutes, nzDisabledSeconds }-
(nzOnOk)点击确定按钮的回调EventEmitter<Date[]>-

nzShowTime 中当前支持的 nz-time-picker 参数有:nzFormat, nzHourStep, nzMinuteStep, nzSecondStep, nzDisabledHours, nzDisabledMinutes, nzDisabledSeconds, nzHideDisabledOptions, nzDefaultOpenValue, nzAddOn

nzFormat 特别说明

日期格式化目前同时支持两种方式:DatePipe(默认,语法参考) 和 date-fns语法参考,见如何使用 date-fns 进行日期格式化)。