Html 如何设置输入类型日期值max=";“可变值”;
无法将“今日日期值”获取到输入字段的“max”属性,但能够在控制台中获取该值 你们能帮助我如何在输入字段中获取值吗(即max=“2018-08-21”)Html 如何设置输入类型日期值max=";“可变值”;,html,angular,typescript,Html,Angular,Typescript,无法将“今日日期值”获取到输入字段的“max”属性,但能够在控制台中获取该值 你们能帮助我如何在输入字段中获取值吗(即max=“2018-08-21”) 当您使用Angular时,您可以将日期绑定为: TS todayDate = new Date().getFullYear() + "-" + ("0" + (new Date().getMonth() + 1)).slice(-2) + "-" + ("0" + new Date().getDate()).slice(-2); HTML
当您使用Angular时,您可以将日期绑定为: TS
todayDate = new Date().getFullYear() + "-" + ("0" + (new Date().getMonth() + 1)).slice(-2) + "-" + ("0" + new Date().getDate()).slice(-2);
HTML
<div class="input-group">
<input type="date" [max]="todayDate" />
</div>
您可以使用
[max]
export class AppComponent {
todayDate = new Date('2018-08-21')
}
<div class="input-group">
<input type="date" [max]="todayDate | date:'yyyy-MM-dd'" />
</div>
导出类AppComponent{
今天日期=新日期('2018-08-21')
}
由于没有人向您提供有角度的答案,而是依赖您的区域设置来测试它(这样做很危险),因此它是:一个自定义验证器,它将检查日期是否低于您提供给验证器的日期
验证器:
export function notAfterToday(date: Date = new Date(Date.now())): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
const selectedDate = new Date(control.value);
return selectedDate && !isNaN(selectedDate.getTime()) && selectedDate.getTime() > date.getTime() ? { 'dateTooHigh': true } : null;
};
在处理日期时,Javascript应该在HTMLY之前,您应该使用
momentjs
。@MalathyVenkatesan很棒,您可以投票并接受答案:)
export function notAfterToday(date: Date = new Date(Date.now())): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
const selectedDate = new Date(control.value);
return selectedDate && !isNaN(selectedDate.getTime()) && selectedDate.getTime() > date.getTime() ? { 'dateTooHigh': true } : null;
};