Set minDate/maxDate dynamically on html datepicker

Viewed 50499

I need to restrict my datepicker so that people who are under 16 cannot select their birthdate on the calendar until the day they turn 16.

I was using Angular datepicker and had no problem to do it with this code in my typescript file:

now = new Date();
year = this.now.getFullYear();
month = this.now.getMonth();
day = this.now.getDay();
minDate = { year: this.year - 100, month: this.month, day: this.day };
maxDate = { year: this.year - 16, month: this.month + 1, day: this.day };

Here is my HTML:

<input class="form-control" [required]="mngVis.birthdate.isrequired" 
type="date" maxDate="maxDate" minDate="minDate" 
jhiTranslate="form.birthdate.placeholder"
ngModel name="birthdate" #birthdate="ngModel">

The issue I have is with the default html datepicker, and I need to use this datepicker and not ngb-datepicker. All the solution I find use jQuery but I cannot use it with Angular 6.

Hope I made myself clear and that you can help me with this !

Thank you.

ETA:

Here is the solution to my issue: app.component.ts :

import { Component } from '@angular/core';
import moment from 'moment'

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';

  now = new Date();
  year = this.now.getFullYear();
  month = this.now.getMonth();
  day = this.now.getDay();
  minDate = moment({year: this.year - 100, month: this.month, day: this.day}).format('YYYY-MM-DD');

  maxDate = moment({year: this.year - 16, month: this.month, day: this.day}).format('YYYY-MM-DD');

  date(ev) {
    console.log(this.minDate)
    console.log(ev.target.value)
  }
}

HTML:

 <input class="form-control" [required]="mngVis.birthdate.isrequired" 
    type="date" [max]="maxDate" [min]="minDate" 
    jhiTranslate="form.birthdate.placeholder"
    ngModel name="birthdate" #birthdate="ngModel">
5 Answers

Using moment here for date formation

DEMO

Reference ----> https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

//Taken current date as min date. and random as max date. 
// Default format of date is as "YYYY-MM-DD" so we need to convert using  moment  

minDate = moment(new Date()).format('YYYY-MM-DD')
maxDate ="2018-09-08"

HTML:

<input class="form-control" type="date" [min]="minDate" [max]="maxDate">

The issue with the HTML datepicker is the same you have with <b>, <i>, <u> ... tags : you expect those tags to behave in some way, but it's up to the browser to treat them how it likes.

This means that <b>bold</b> can be bold on Chrome, but not on IE.

The same happens with the date input : sometimes your user won't be able to have a calendar to pick from (but still an input).

The advantage of datepickers is that they display this calendar with plain old HTML/CSS/JS, leaving you with something that will work across most browsers.

If you don't want to use one, suit yourself : but how will you implement validation ?

Instead of locking the dates in the picker, consider using Angular's validation on the control :

<input class="form-control" type="date" max="maxDate" min="minDate" name="birthdate" 
  [required]="mngVis.birthdate.isrequired"
  jhiTranslate="form.birthdate.placeholder" 
  [formControl]="birthdate">
birthdate = new FormControl('', [Validators.max(this.birthdate), Validators.min(minDate)])

Hi @tiphanie If you want to use jQuery datepicker instead of ngb-datepicker.

You can set MaxDate and MinDate using the jQuery datepicker Api reference Method 'option'

.datepicker( 'option', 'minDate', new Date(2018, 1 - 1, 4))
.datepicker('option','maxDate', new Date(2018, 1 - 1, 24))

Check my working link :Datepicker jQuery UI in Angular 6

You need to add max and min attribute for this.

Demo on Stackblitz

component.ts

maxDate="2018-08-28";
minDate:'2016-08-28';

component.html

<input type="date"  [min]="minDate | date:'yyyy-MM-dd'" [max]="maxDate | date:'yyyy-MM-dd'" >

//Add maxDate variable inside component class

maxDate = new Date(new Date().getTime() + 86400000).toISOString().substring(0, 10);

// convert to substring because datepicker format

<input type="date" id="dateSlot" class="form-control app-inputfield" [min]="maxDate" placeholder="mm/d/year" onkeydown="return false" [(ngModel)]="yourModelName" [ngModelOptions]="{standalone: true}">
Related