antDesign 【NG-ZORRO、Angular】日期选择框时间段nz-range-picker设置默认选择日期及限制日期可选范围
下面的代码包含
1.只可以选择今天以后
2.只可以选择今天开始一周内
3.只能选择今天之前的
import { Component } from '@angular/core'; import differenceInCalendarDays from 'date-fns/differenceInCalendarDays'; import setHours from 'date-fns/setHours'; import { DisabledTimeFn, DisabledTimePartial } from 'ng-zorro-antd/date-picker'; @Component({ selector: 'nz-demo-date-picker-disabled-date', template: ` <nz-date-picker nzFormat="yyyy-MM-dd HH:mm:ss" [nzDisabledDate]="disabledDate" [nzDisabledTime]="disabledDateTime" [nzShowTime]="{ nzDefaultOpenValue: timeDefaultValue }" > </nz-date-picker> <br /> <nz-month-picker [nzDisabledDate]="disabledDate" nzPlaceHolder="Select month"></nz-month-picker> <br /> <nz-year-picker [nzDisabledDate]="disabledDate" nzPlaceHolder="Select year"></nz-year-picker> <br /> <nz-range-picker [nzDisabledDate]="disabledDate" [nzDisabledTime]="disabledRangeTime" [nzShowTime]="{ nzHideDisabledOptions: true, nzDefaultOpenValue: timeDefaultValue }" nzFormat="yyyy-MM-dd HH:mm:ss" ></nz-range-picker> `, styles: [ ` nz-date-picker, nz-month-picker, nz-year-picker, nz-range-picker, nz-week-picker { margin: 0 8px 12px 0; } ` ] }) export class NzDemoDatePickerDisabledDateComponent { today = new Date(); timeDefaultValue = setHours(new Date(), 0); range(start: number, end: number): number[] { const result: number[] = []; for (let i = start; i < end; i++) { result.push(i); } return result; } disabledDate = (current: Date): boolean => { // 只能选择今天之前的 return differenceInCalendarDays(current, this.today) > 0; }; disabledDateTime: DisabledTimeFn = () => { return { nzDisabledHours: () => this.range(0, 24).splice(4, 20), nzDisabledMinutes: () => this.range(30, 60), nzDisabledSeconds: () => [55, 56] }; };
public disabledDate1 = (current: Date): boolean => {
console.log('current',current)
console.log('111',this._mycom.setDate(current))
console.log('this.today',this.today)
console.log('differenceInCalendarDays(current, this.today)',differenceInCalendarDays(current, this.today))
return differenceInCalendarDays(current, this.today) > 6 || differenceInCalendarDays(current, this.today) < 0 ; // 只可以选择今天开始一周内
};
public disabledDate2 = (current: Date): boolean => {
return differenceInCalendarDays(current, this.today) < 0; // 只可以选择今天以后
};
disabledRangeTime: DisabledTimeFn = (_value, type?: DisabledTimePartial) => { if (type === 'start') { return { nzDisabledHours: () => this.range(0, 60).splice(4, 20), nzDisabledMinutes: () => this.range(30, 60), nzDisabledSeconds: () => [55, 56] }; } return { nzDisabledHours: () => this.range(0, 60).splice(20, 4), nzDisabledMinutes: () => this.range(0, 31), nzDisabledSeconds: () => [55, 56] }; }; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决