Date range in Angular Calendar component
27 Apr 20242 minutes to read
You can restrict the user to select the date from a specified range of dates by utilizing the min
and max
properties. Always the min date has to be lesser than the max date.
The below example allows you to select a date within a range from 7th to 27th days in a month.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { CalendarModule } from '@syncfusion/ej2-angular-calendars'
import { Component } from '@angular/core';
@Component({
imports: [
CalendarModule //declaration of ej2-angular-calendars module into NgModule
],
standalone: true,
selector: 'app-root',
template: `
<!--Sets the value, min and max -->
<ejs-calendar [value]='dateValue' [min]='minDate' [max]='maxDate'></ejs-calendar>
`
})
export class AppComponent {
public today: Date = new Date();
public currentYear: number = this.today.getFullYear();
public currentMonth: number = this.today.getMonth();
public currentDay: number = this.today.getDate();
public dateValue: Object = new Date(new Date().setDate(14));
public minDate: Object = new Date(this.currentYear, this.currentMonth, 7);
public maxDate: Object = new Date(this.currentYear, this.currentMonth, 27);
constructor() {
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
If the value of
min
ormax
properties changed through code behind, then you have to update thevalue
property to set within the range. Or else, if the value is out of specified date range and less thanmin
date, value property will be updated with min date or the value is higher than max date, value property will be updated withmax
date.