Angular 2 Ionic 2 - How to set the maximum or minimum date for today to enter the date?

 <input class="alert-input date-input" #dob="ngModel" name="dob" max="2018-03-07" [(ngModel)]="leadDetail.dob" type="date"></div>

How to set the maximum date for today instead of 2018-03-07 dynamically?

I tried the 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.

+6
source share
3 answers

Try the following:

<input class="alert-input date-input" name="dob" [max]="today" type="date">


today = new Date().toJSON().split('T')[0];

Cause:

Beware when you use it new Date(), it will give you the full date with time zone and time, etc., you only need to assign a date so that you only share it using Date. to complete this more fully

console.log(new Date(), '----', new Date().toJSON());
+13
source

, , , , ngModel

0

the submit button will be disabled if the entered date is greater than the current date

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

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")
}
}
0
source

Source: https://habr.com/ru/post/1674651/


All Articles