DatePicker日期选择
用于选择日期或者时间。
规则
- 最多展示 5 个独立滚轮,每个滚轮表示一个不同的值。
代码演示
日期-年月日时分
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'demo-date-picker-basic',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[extra]="currentDateFormat(value)"
[arrow]="'horizontal'"
[mode]="'datetime'"
[(ngModel)]="value"
(onOk)="onOk($event)"
>
Datetime
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerBasicComponent {
name = '选择';
value = new Date();
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result);
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
日期-年月日
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'demo-date-picker-date',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[extra]="currentDateFormat(value, 'yyyy-mm-dd')"
[arrow]="'horizontal'"
[mode]="'date'"
[(ngModel)]="value"
(onOk)="onOk($event)"
>
Date
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerDateComponent {
name = '选择';
value = new Date();
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result, 'yyyy-mm-dd');
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
日期-年月
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'demo-date-picker-month',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[extra]="currentDateFormat(value, 'yyyy-mm')"
[arrow]="'horizontal'"
[mode]="'month'"
[(ngModel)]="value"
(onOk)="onOk($event)"
>
Month
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerMonthComponent {
name = '选择';
value = new Date(2019, 4);
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result);
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
日期-时分
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'demo-date-picker-time',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[extra]="currentDateFormat(value)"
[arrow]="'horizontal'"
[mode]="'time'"
[(ngModel)]="value"
(onOk)="onOk($event)"
>
Time
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerTimeComponent {
name = '选择';
value = new Date();
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result);
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
日期-时分
import { Component, ViewEncapsulation } from '@angular/core';
import { en_US } from 'ng-zorro-antd-mobile';
@Component({
selector: 'demo-date-picker-utc-time',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[extra]="currentDateFormat(value)"
[arrow]="'horizontal'"
[mode]="'time'"
[locale]="locale"
[(ngModel)]="value"
(onOk)="onOk($event)"
>
UTC Time
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerUtcTimeComponent {
locale = en_US;
name = '选择';
nowTimeStamp = Date.now();
now = new Date(this.nowTimeStamp);
utcNow = new Date(this.now.getTime() + this.now.getTimezoneOffset() * 60000);
value = this.utcNow;
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result);
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
当前时间小于最小时间
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'demo-date-picker-min-date',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[extra]="currentDateFormat(value)"
[arrow]="'horizontal'"
[mode]="'datetime'"
[(ngModel)]="value"
(onOk)="onOk($event)"
>
Min Date
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerMinDateComponent {
name = '当前时间小于最小时间';
value = new Date(1999, 1, 1, 1, 1);
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result);
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
当前时间大于最大时间
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'demo-date-picker-max-date',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[extra]="currentDateFormat(value)"
[arrow]="'horizontal'"
[mode]="'datetime'"
[(ngModel)]="value"
(onOk)="onOk($event)"
>
Max Date
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerMaxDateComponent {
name = '当前时间大于最大时间';
value = new Date(2031, 1, 1, 1, 1);
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result);
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
设置禁止使用
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'demo-date-picker-disable',
encapsulation: ViewEncapsulation.None,
template: `
<List [className]="'date-picker-list'">
<ListItem
DatePicker
[disabled]="true"
[extra]="currentDateFormat(value)"
[arrow]="'horizontal'"
[mode]="'datetime'"
(onOk)="onOk($event)"
>
Datetime
<Brief>{{ name }}</Brief>
</ListItem>
</List>
`,
styles: [
`
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
`
]
})
export class DemoDatePickerDisableComponent {
name = '选择';
value = new Date();
currentDateFormat(date, format: string = 'yyyy-mm-dd HH:MM'): any {
const pad = (n: number): string => (n < 10 ? `0${n}` : n.toString());
return format
.replace('yyyy', date.getFullYear())
.replace('mm', pad(date.getMonth() + 1))
.replace('dd', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('MM', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
}
onOk(result: Date) {
this.name = this.currentDateFormat(result);
this.value = result;
}
formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
}
API
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[mode] | 日期选择的类型 | ‘year’ | ‘month’ | ‘date’ | ‘time’ | ‘datetime’ | ‘date’ |
[minDate] | 最小可选日期 | Date | 2000-1-1 |
[maxDate] | 最大可选日期 | Date | 2030-1-1 |
[minuteStep] | 分钟数递增步长设置 | number | 1 |
[locale] | 国际化,可覆盖全局LocaleProvider 的配置 | {DatePickerLocale: {year, month, day, hour, minute, am?, pm?}, okText, dismissText } | - |
[disabled] | 是否不可用 | boolean | false |
[showErrorToast] | 显示Toast错误信息 | boolean | true |
[showErrorToastInterval] | Toast错误信息显示时间 | number | 2000 |
[title] | 弹框的标题 | string | TemplateRef | - |
[(ngModel)] | 当前选中时间 | Date | new Date() |
(onValueChange) | 每列 picker 改变时的回调 | EventEmitter<{date: object, index: string}> | - |
(onOk) | 点击选中时执行的回调 | EventEmitter<Date> | - |
(onDismiss) | 点击取消时执行的回调 | EventEmitter<void> | - |
注意:日期字符串在不同浏览器有不同的实现,例如 new Date('2017-1-1')
在 Safari 上是 Invalid Date,而在 Chrome 上是能正常解析的。
注意:DatePicker
children 建议是 ListItem
, 如果不是,需要是自定义组件(组件内需处理 onClick
/ extra
/ children
属性)