[英]Angular 2 Ionic 2 - How to set max or min date to today for date input?
<input class="alert-input date-input" #dob="ngModel" name="dob" max="2018-03-07" [(ngModel)]="leadDetail.dob" type="date"></div>
How can I set the max date for today instead of 2018-03-07 dynamically? 如何动态设置今天的最大日期而不是2018-03-07?
I tried following methods- 我试过以下方法 -
<input max="today" type="date"></div>
<input max="{{today | date:'yyyy-mm-dd'}}" type="date"></div>
Class - 课程 -
public today = new Date();
but no luck. 但没有运气。
Try this: 尝试这个:
<input class="alert-input date-input" name="dob" [max]="today" type="date">
today = new Date().toJSON().split('T')[0];
Reason:
原因:
Becasue when you are using new Date()
this will give you full date with time zone and time etc, you have to assign only Date so you have to split this with only Date. 当您使用
new Date()
这将为您提供时区和时间等的完整日期,您必须仅指定日期,因此您必须将其与仅日期分开。 for more clerification run this 为了更多的工作,运行这个
console.log(new Date(), '----', new Date().toJSON());
将mm更改为MM然后格式但这将影响您更改的日期,这将无助于除非其ngModel绑定到另一个变量
the submit button will be disabled if input date is greater than current date
如果输入日期大于当前日期,则将禁用提交按钮
register.component.html register.component.html
<div class="container">
<form #loginForm="ngForm" (ngSubmit)="submitLoginForm(loginForm)" style="background- color:beige;">
<input [(ngModel)]="vdate" name="dob" type="date">
<button type="submit" [disabled]="!loginForm.valid || (today < vdate)" class="btn">Login</button>
</form>
</div>
register.component.ts register.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
vdate: Date
today = new Date().toJSON().split('T')[0];
constructor() {
}
ngOnInit() {
}
submitLoginForm() {
console.log("Welcome to Jollywood")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.