• DatePicker日期选择
    • 规则
  • 代码演示
  • API

    DatePicker日期选择

    用于选择日期或者时间。

    规则

    • 最多展示 5 个独立滚轮,每个滚轮表示一个不同的值。

    代码演示

    日期-年月日时分

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'demo-date-picker-basic',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <List [className]="'date-picker-list'">
    7. <ListItem
    8. DatePicker
    9. [extra]="currentDateFormat(value)"
    10. [arrow]="'horizontal'"
    11. [mode]="'datetime'"
    12. [(ngModel)]="value"
    13. (onOk)="onOk($event)"
    14. >
    15. Datetime
    16. <Brief>{{ name }}</Brief>
    17. </ListItem>
    18. </List>
    19. `,
    20. styles: [
    21. `
    22. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    23. flex-basis: initial;
    24. }
    25. `
    26. ]
    27. })
    28. export class DemoDatePickerBasicComponent {
    29. name = '选择';
    30. value = new Date();
    31. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    32. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    33. return format
    34. .replace('yyyy', date.getFullYear())
    35. .replace('mm', pad(date.getMonth() + 1))
    36. .replace('dd', pad(date.getDate()))
    37. .replace('HH', pad(date.getHours()))
    38. .replace('MM', pad(date.getMinutes()))
    39. .replace('ss', pad(date.getSeconds()));
    40. }
    41. onOk(result: Date) {
    42. this.name = this.currentDateFormat(result);
    43. this.value = result;
    44. }
    45. formatIt(date: Date, form: string) {
    46. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    47. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    48. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    49. if (form === 'YYYY-MM-DD') {
    50. return dateStr;
    51. }
    52. if (form === 'HH:mm') {
    53. return timeStr;
    54. }
    55. return `${dateStr} ${timeStr}`;
    56. }
    57. }

    日期-年月日

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'demo-date-picker-date',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <List [className]="'date-picker-list'">
    7. <ListItem
    8. DatePicker
    9. [extra]="currentDateFormat(value, 'yyyy-mm-dd')"
    10. [arrow]="'horizontal'"
    11. [mode]="'date'"
    12. [(ngModel)]="value"
    13. (onOk)="onOk($event)"
    14. >
    15. Date
    16. <Brief>{{ name }}</Brief>
    17. </ListItem>
    18. </List>
    19. `,
    20. styles: [
    21. `
    22. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    23. flex-basis: initial;
    24. }
    25. `
    26. ]
    27. })
    28. export class DemoDatePickerDateComponent {
    29. name = '选择';
    30. value = new Date();
    31. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    32. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    33. return format
    34. .replace('yyyy', date.getFullYear())
    35. .replace('mm', pad(date.getMonth() + 1))
    36. .replace('dd', pad(date.getDate()))
    37. .replace('HH', pad(date.getHours()))
    38. .replace('MM', pad(date.getMinutes()))
    39. .replace('ss', pad(date.getSeconds()));
    40. }
    41. onOk(result: Date) {
    42. this.name = this.currentDateFormat(result, 'yyyy-mm-dd');
    43. this.value = result;
    44. }
    45. formatIt(date: Date, form: string) {
    46. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    47. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    48. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    49. if (form === 'YYYY-MM-DD') {
    50. return dateStr;
    51. }
    52. if (form === 'HH:mm') {
    53. return timeStr;
    54. }
    55. return `${dateStr} ${timeStr}`;
    56. }
    57. }

    日期-年月

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'demo-date-picker-month',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <List [className]="'date-picker-list'">
    7. <ListItem
    8. DatePicker
    9. [extra]="currentDateFormat(value, 'yyyy-mm')"
    10. [arrow]="'horizontal'"
    11. [mode]="'month'"
    12. [(ngModel)]="value"
    13. (onOk)="onOk($event)"
    14. >
    15. Month
    16. <Brief>{{ name }}</Brief>
    17. </ListItem>
    18. </List>
    19. `,
    20. styles: [
    21. `
    22. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    23. flex-basis: initial;
    24. }
    25. `
    26. ]
    27. })
    28. export class DemoDatePickerMonthComponent {
    29. name = '选择';
    30. value = new Date(2019, 4);
    31. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    32. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    33. return format
    34. .replace('yyyy', date.getFullYear())
    35. .replace('mm', pad(date.getMonth() + 1))
    36. .replace('dd', pad(date.getDate()))
    37. .replace('HH', pad(date.getHours()))
    38. .replace('MM', pad(date.getMinutes()))
    39. .replace('ss', pad(date.getSeconds()));
    40. }
    41. onOk(result: Date) {
    42. this.name = this.currentDateFormat(result);
    43. this.value = result;
    44. }
    45. formatIt(date: Date, form: string) {
    46. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    47. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    48. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    49. if (form === 'YYYY-MM-DD') {
    50. return dateStr;
    51. }
    52. if (form === 'HH:mm') {
    53. return timeStr;
    54. }
    55. return `${dateStr} ${timeStr}`;
    56. }
    57. }

    日期-时分

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'demo-date-picker-time',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <List [className]="'date-picker-list'">
    7. <ListItem
    8. DatePicker
    9. [extra]="currentDateFormat(value)"
    10. [arrow]="'horizontal'"
    11. [mode]="'time'"
    12. [(ngModel)]="value"
    13. (onOk)="onOk($event)"
    14. >
    15. Time
    16. <Brief>{{ name }}</Brief>
    17. </ListItem>
    18. </List>
    19. `,
    20. styles: [
    21. `
    22. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    23. flex-basis: initial;
    24. }
    25. `
    26. ]
    27. })
    28. export class DemoDatePickerTimeComponent {
    29. name = '选择';
    30. value = new Date();
    31. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    32. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    33. return format
    34. .replace('yyyy', date.getFullYear())
    35. .replace('mm', pad(date.getMonth() + 1))
    36. .replace('dd', pad(date.getDate()))
    37. .replace('HH', pad(date.getHours()))
    38. .replace('MM', pad(date.getMinutes()))
    39. .replace('ss', pad(date.getSeconds()));
    40. }
    41. onOk(result: Date) {
    42. this.name = this.currentDateFormat(result);
    43. this.value = result;
    44. }
    45. formatIt(date: Date, form: string) {
    46. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    47. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    48. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    49. if (form === 'YYYY-MM-DD') {
    50. return dateStr;
    51. }
    52. if (form === 'HH:mm') {
    53. return timeStr;
    54. }
    55. return `${dateStr} ${timeStr}`;
    56. }
    57. }

    日期-时分

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. import { en_US } from 'ng-zorro-antd-mobile';
    3. @Component({
    4. selector: 'demo-date-picker-utc-time',
    5. encapsulation: ViewEncapsulation.None,
    6. template: `
    7. <List [className]="'date-picker-list'">
    8. <ListItem
    9. DatePicker
    10. [extra]="currentDateFormat(value)"
    11. [arrow]="'horizontal'"
    12. [mode]="'time'"
    13. [locale]="locale"
    14. [(ngModel)]="value"
    15. (onOk)="onOk($event)"
    16. >
    17. UTC Time
    18. <Brief>{{ name }}</Brief>
    19. </ListItem>
    20. </List>
    21. `,
    22. styles: [
    23. `
    24. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    25. flex-basis: initial;
    26. }
    27. `
    28. ]
    29. })
    30. export class DemoDatePickerUtcTimeComponent {
    31. locale = en_US;
    32. name = '选择';
    33. nowTimeStamp = Date.now();
    34. now = new Date(this.nowTimeStamp);
    35. utcNow = new Date(this.now.getTime() + this.now.getTimezoneOffset() * 60000);
    36. value = this.utcNow;
    37. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    38. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    39. return format
    40. .replace('yyyy', date.getFullYear())
    41. .replace('mm', pad(date.getMonth() + 1))
    42. .replace('dd', pad(date.getDate()))
    43. .replace('HH', pad(date.getHours()))
    44. .replace('MM', pad(date.getMinutes()))
    45. .replace('ss', pad(date.getSeconds()));
    46. }
    47. onOk(result: Date) {
    48. this.name = this.currentDateFormat(result);
    49. this.value = result;
    50. }
    51. formatIt(date: Date, form: string) {
    52. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    53. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    54. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    55. if (form === 'YYYY-MM-DD') {
    56. return dateStr;
    57. }
    58. if (form === 'HH:mm') {
    59. return timeStr;
    60. }
    61. return `${dateStr} ${timeStr}`;
    62. }
    63. }

    当前时间小于最小时间

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'demo-date-picker-min-date',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <List [className]="'date-picker-list'">
    7. <ListItem
    8. DatePicker
    9. [extra]="currentDateFormat(value)"
    10. [arrow]="'horizontal'"
    11. [mode]="'datetime'"
    12. [(ngModel)]="value"
    13. (onOk)="onOk($event)"
    14. >
    15. Min Date
    16. <Brief>{{ name }}</Brief>
    17. </ListItem>
    18. </List>
    19. `,
    20. styles: [
    21. `
    22. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    23. flex-basis: initial;
    24. }
    25. `
    26. ]
    27. })
    28. export class DemoDatePickerMinDateComponent {
    29. name = '当前时间小于最小时间';
    30. value = new Date(1999, 1, 1, 1, 1);
    31. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    32. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    33. return format
    34. .replace('yyyy', date.getFullYear())
    35. .replace('mm', pad(date.getMonth() + 1))
    36. .replace('dd', pad(date.getDate()))
    37. .replace('HH', pad(date.getHours()))
    38. .replace('MM', pad(date.getMinutes()))
    39. .replace('ss', pad(date.getSeconds()));
    40. }
    41. onOk(result: Date) {
    42. this.name = this.currentDateFormat(result);
    43. this.value = result;
    44. }
    45. formatIt(date: Date, form: string) {
    46. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    47. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    48. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    49. if (form === 'YYYY-MM-DD') {
    50. return dateStr;
    51. }
    52. if (form === 'HH:mm') {
    53. return timeStr;
    54. }
    55. return `${dateStr} ${timeStr}`;
    56. }
    57. }

    当前时间大于最大时间

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'demo-date-picker-max-date',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <List [className]="'date-picker-list'">
    7. <ListItem
    8. DatePicker
    9. [extra]="currentDateFormat(value)"
    10. [arrow]="'horizontal'"
    11. [mode]="'datetime'"
    12. [(ngModel)]="value"
    13. (onOk)="onOk($event)"
    14. >
    15. Max Date
    16. <Brief>{{ name }}</Brief>
    17. </ListItem>
    18. </List>
    19. `,
    20. styles: [
    21. `
    22. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    23. flex-basis: initial;
    24. }
    25. `
    26. ]
    27. })
    28. export class DemoDatePickerMaxDateComponent {
    29. name = '当前时间大于最大时间';
    30. value = new Date(2031, 1, 1, 1, 1);
    31. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    32. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    33. return format
    34. .replace('yyyy', date.getFullYear())
    35. .replace('mm', pad(date.getMonth() + 1))
    36. .replace('dd', pad(date.getDate()))
    37. .replace('HH', pad(date.getHours()))
    38. .replace('MM', pad(date.getMinutes()))
    39. .replace('ss', pad(date.getSeconds()));
    40. }
    41. onOk(result: Date) {
    42. this.name = this.currentDateFormat(result);
    43. this.value = result;
    44. }
    45. formatIt(date: Date, form: string) {
    46. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    47. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    48. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    49. if (form === 'YYYY-MM-DD') {
    50. return dateStr;
    51. }
    52. if (form === 'HH:mm') {
    53. return timeStr;
    54. }
    55. return `${dateStr} ${timeStr}`;
    56. }
    57. }

    设置禁止使用

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'demo-date-picker-disable',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <List [className]="'date-picker-list'">
    7. <ListItem
    8. DatePicker
    9. [disabled]="true"
    10. [extra]="currentDateFormat(value)"
    11. [arrow]="'horizontal'"
    12. [mode]="'datetime'"
    13. (onOk)="onOk($event)"
    14. >
    15. Datetime
    16. <Brief>{{ name }}</Brief>
    17. </ListItem>
    18. </List>
    19. `,
    20. styles: [
    21. `
    22. .date-picker-list .am-list-item .am-list-line .am-list-extra {
    23. flex-basis: initial;
    24. }
    25. `
    26. ]
    27. })
    28. export class DemoDatePickerDisableComponent {
    29. name = '选择';
    30. value = new Date();
    31. currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
    32. const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
    33. return format
    34. .replace('yyyy', date.getFullYear())
    35. .replace('mm', pad(date.getMonth() + 1))
    36. .replace('dd', pad(date.getDate()))
    37. .replace('HH', pad(date.getHours()))
    38. .replace('MM', pad(date.getMinutes()))
    39. .replace('ss', pad(date.getSeconds()));
    40. }
    41. onOk(result: Date) {
    42. this.name = this.currentDateFormat(result);
    43. this.value = result;
    44. }
    45. formatIt(date: Date, form: string) {
    46. const pad = (n: number) => (n < 10 ? `0${n}` : n);
    47. const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    48. const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
    49. if (form === 'YYYY-MM-DD') {
    50. return dateStr;
    51. }
    52. if (form === 'HH:mm') {
    53. return timeStr;
    54. }
    55. return `${dateStr} ${timeStr}`;
    56. }
    57. }

    DatePicker 日期选择 - 图1

    API

    属性说明类型默认值
    mode日期选择的类型, 可以是日期date,时间time,日期+时间datetime,年year,月monthStringdate
    ngModel当前选中时间Date当前时间
    minDate最小可选日期Date2000-1-1
    maxDate最大可选日期Date2030-1-1
    minuteStep分钟数递增步长设置Number1
    locale国际化,可覆盖全局LocaleProvider的配置Object: {DatePickerLocale: {year, month, day, hour, minute, am?, pm?}, okText, dismissText }-
    disabled是否不可用Booleanfalse
    showErrorToast显示Toast错误信息Booleantrue
    showErrorToastIntervalToast错误信息显示时间number2000
    onValueChange每列 picker 改变时的回调({date: any, index: number}) => void-
    title弹框的标题string/React.TemplateRef
    onOk点击选中时执行的回调(val): void
    onDismiss点击取消时执行的回调(): void

    注意:日期字符串在不同浏览器有不同的实现,例如 new Date('2017-1-1') 在 Safari 上是 Invalid Date,而在 Chrome 上是能正常解析的。

    注意:DatePicker children 建议是 ListItem, 如果不是,需要是自定义组件(组件内需处理 onClick / extra / children 属性)