In Angular, you can dynamically set the minimum and maximum dates in a date picker by binding the min and max attributes to properties in your component. This approach works with both native HTML5 date inputs and various Angular UI libraries that offer date pickers, such as Angular Material, ng-bootstrap, or ngx-bootstrap.
Here's a basic example using the native HTML5 date input:
<input type="date" [min]="minDate" [max]="maxDate">
import { Component } from '@angular/core'; @Component({ selector: 'app-date-picker', templateUrl: './date-picker.component.html' }) export class DatePickerComponent { minDate: string; maxDate: string; constructor() { // Set the min and max dates let today = new Date(); this.minDate = this.formatDate(today); // Set max date to 7 days from today let maxDate = new Date(); maxDate.setDate(maxDate.getDate() + 7); this.maxDate = this.formatDate(maxDate); } private formatDate(date: Date): string { // Format date to 'yyyy-MM-dd' let d = new Date(date), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('-'); } } In this example:
minDate and maxDate are properties of the component class, set to strings formatted as yyyy-MM-dd, which is the format required for the min and max attributes of the <input type="date">.formatDate method formats a JavaScript Date object into a string in the yyyy-MM-dd format.minDate is set to today's date, and maxDate is set to 7 days from today. You can adjust these as needed.If you're using Angular Material, you can bind the min and max properties in a similar way:
<mat-form-field> <input matInput [matDatepicker]="picker" [min]="minDate" [max]="maxDate"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker> </mat-form-field>
The Angular component code would remain the same as in the previous example.
Date object uses the user's local time zone.Set Min Max Date Dynamically in Angular Datepicker:
<!-- HTML Template --> <input [min]="minDate" [max]="maxDate" matInput [matDatepicker]="picker"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker>
// TypeScript Component import { Component } from '@angular/core'; import { MatDatepickerInputEvent } from '@angular/material/datepicker'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: Date = new Date(); maxDate: Date = new Date(); onDateChange(event: MatDatepickerInputEvent<Date>): void { this.minDate = event.value || new Date(); this.maxDate = new Date(this.minDate.getFullYear() + 1, this.minDate.getMonth(), this.minDate.getDate()); } } Dynamically Change Datepicker Range in Angular:
<!-- HTML Template --> <input [min]="minDate" [max]="maxDate" [(ngModel)]="selectedDate" placeholder="Select a date" />
// TypeScript Component import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: Date = new Date(); maxDate: Date = new Date(); selectedDate: Date = new Date(); constructor() { this.maxDate.setDate(this.minDate.getDate() + 30); // Set max date to 30 days from today } } ngModel to dynamically change the datepicker range.Angular Datepicker Dynamic minDate and maxDate:
<!-- HTML Template --> <input [min]="minDate" [max]="maxDate" matInput [matDatepicker]="picker" [(ngModel)]="selectedDate"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker>
// TypeScript Component import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: Date = new Date(); maxDate: Date = new Date(); selectedDate: Date = new Date(); constructor() { this.maxDate.setDate(this.minDate.getDate() + 30); // Set max date to 30 days from today } } Update Datepicker Min and Max Date Programmatically:
<!-- HTML Template --> <input [min]="minDate" [max]="maxDate" [(ngModel)]="selectedDate" placeholder="Select a date" />
// TypeScript Component import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: Date = new Date(); maxDate: Date = new Date(); selectedDate: Date = new Date(); constructor() { this.maxDate.setDate(this.minDate.getDate() + 30); // Set max date to 30 days from today } } Change mat-datepicker Min and Max Date Angular:
<!-- HTML Template --> <input [min]="minDate" [max]="maxDate" matInput [matDatepicker]="picker" [(ngModel)]="selectedDate"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker>
// TypeScript Component import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: Date = new Date(); maxDate: Date = new Date(); selectedDate: Date = new Date(); constructor() { this.maxDate.setDate(this.minDate.getDate() + 30); // Set max date to 30 days from today } } Angular Dynamic Date Range in Datepicker:
<!-- HTML Template --> <input [min]="minDate" [max]="maxDate" matInput [matDatepicker]="picker" [(ngModel)]="selectedDate"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker>
// TypeScript Component import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: Date = new Date(); maxDate: Date = new Date(); selectedDate: Date = new Date(); constructor() { this.maxDate.setDate(this.minDate.getDate() + 30); // Set max date to 30 days from today } } Dynamically Set ng-bootstrap Datepicker Min Max Date:
<!-- HTML Template --> <input [(ngModel)]="selectedDate" [ngbDatepicker]="dp" #d="ngbDatepicker" /> <button (click)="open(d)">Open</button>
// TypeScript Component import { Component } from '@angular/core'; import { NgbDateStruct, NgbCalendar } from '@ng-bootstrap/ng-bootstrap'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: NgbDateStruct; maxDate: NgbDateStruct; selectedDate: NgbDateStruct; constructor(private calendar: NgbCalendar) { this.minDate = this.calendar.getToday(); this.maxDate = this.calendar.getNext(this.calendar.getToday(), 'd', 30); // Set max date to 30 days from today } open(d: any): void { d.toggle(); } } Angular Reactive Form Datepicker Dynamic Range:
<!-- HTML Template --> <form [formGroup]="myForm"> <input formControlName="selectedDate" matInput [matDatepicker]="picker"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-datepicker> </form>
// TypeScript Component import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent implements OnInit { myForm: FormGroup; constructor(private fb: FormBuilder) {} ngOnInit(): void { this.myForm = this.fb.group({ selectedDate: [new Date()], }); } } Dynamically Update PrimeNG Datepicker Min Max Date:
<!-- HTML Template --> <input pInputText [ngModel]="selectedDate" (ngModelChange)="onDateChange($event)" /> <p-calendar [(ngModel)]="selectedDate" [minDate]="minDate" [maxDate]="maxDate"></p-calendar>
// TypeScript Component import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { minDate: Date = new Date(); maxDate: Date = new Date(); selectedDate: Date = new Date(); constructor() { this.maxDate.setDate(this.minDate.getDate() + 30); // Set max date to 30 days from today } onDateChange(event: Date): void { this.selectedDate = event; } } flowlayout glsl captcha patch filefield custom-scrolling rowcount xcodebuild android-logcat marionette