How to set minDate for ng-bootstrap datepicker

29,856

Solution 1

From your code snippet it looks like you are using ng-bootstrap from https://ng-bootstrap.github.io/#/components/datepicker. Assuming that I'm correct you need to use NgbDateStruct (an object with fields like year, month, day) instead of the built-in JavaScript Date object. For example, to set a min date to the beginning of this year you would use:

this.minDate = {year: 2017, month: 1, day: 1};

Here is a working example in a plunker: http://plnkr.co/edit/VDEYDwp7QIZDDHatCNPh?p=preview

Solution 2

Just pass [minDate]="{year: 1980, month: 1, day: 1}":

<input type="text" [(ngModel)]="date"  ngbDatepicker #d="ngbDatepicker" (focus)="d.open()" [minDate]="{year: 1980, month: 1, day: 1}" name="date" class="form-control"/>
Share:
29,856
Anshuman Jasrotia
Author by

Anshuman Jasrotia

With more than 10 years of experience in Full Software Development Lifecycle(SDLC), I am adept at handling all the challenges associated with software development in tight deadlines. I have excellent analytical and decision-making skills with the ability to take charge when needed. My background includes coordinating and leading myriad teams to achieve the defined organizational goals.

Updated on July 05, 2022

Comments

  • Anshuman Jasrotia
    Anshuman Jasrotia almost 2 years

    Below is the code I am using to set the minDate

     <input ngbDatepicker #d="ngbDatepicker"  [minDate]="minDate" [formControl]="Form.controls['dateOfBirth']">
    

    In the component I am setting the minDate as below:

    public minDate: Date = void 0; 
    constructor() {
        this.minDate = new Date(1950, 1, 1);
    }
    

    But the datepicker still shows 2007 as the minDate. Is there any thing that is missing. Let me know in case any other information is required.

    Thanks for your time.