Search results

Getting started with Angular DateRangePicker component

22 Jul 2021 / 3 minutes to read

The following section explains the steps required to create a simple DateRangePicker component and also it demonstrates the basic usage of the DateRangePicker.

Dependencies

Install the below required dependency packages in order to use the DateRangePicker component in your application.

Copied to clipboard
|-- @syncfusion/ej2-angular-calendars
    |-- @syncfusion/ej2-angular-base
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-calendars
        |-- @syncfusion/ej2-lists
        |-- @syncfusion/ej2-inputs
          |-- @syncfusion/ej2-splitbuttons
        |-- @syncfusion/ej2-popups
            |-- @syncfusion/ej2-buttons

Setup Angular environment

Angular provides the easiest way to set angular CLI projects using Angular CLI tool.

Install the CLI application globally to your machine.

Copied to clipboard
npm install -g @angular/cli

Create a new application

Copied to clipboard
ng new syncfusion-angular-daterangepicker

By default, it install the CSS style base application. To setup with SCSS, pass —style=scss argument on create project.

Example code snippet.

Copied to clipboard
ng new syncfusion-angular-daterangepicker --style=scss

Navigate to the created project folder.

Copied to clipboard
cd syncfusion-angular-daterangepicker

Adding DateRangePicker package

All the available Essential JS 2 packages are published in npmjs.com registry.

To install DateRangePicker component, use the following command.

Copied to clipboard
npm install @syncfusion/ej2-angular-calendars --save
(or)
npm I @syncfusion/ej2-angular-calendars --save

The —save will instruct NPM to include the DateRangePicker package inside of the dependencies section of the package.json.

Registering DateRangePicker module

Import DateRangePicker module into Angular application(src/app/app.module.ts) from the package @syncfusion/ej2-angular-calendars.

Copied to clipboard
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the DateRangePickerModule for the DateRangPicker component
import { DateRangePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent }  from './app.component';

@NgModule({
  //declaration of DateRangePickerModule into NgModule
  imports:      [ BrowserModule, DateRangePickerModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Adding CSS reference

The following CSS files are available in ../node_modules/@syncfusion package folder. This can be referenced in [src/styles.css] using following code.

Copied to clipboard
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-calendars/styles/material.css';
@import '../node_modules/@syncfusion/ej2-angular-calendars/styles/material.css';

If you want to refer the combined component styles, please make use of our CRG (Custom Resource Generator) in your application.

Adding DateRangePicker component

Modify the template in [src/app/app.component.ts] file to render the DateRangePicker component. by using <ejs-daterangepicker> selector.

Copied to clipboard
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<!-- To Render DateRangePicker -->
             <ejs-daterangepicker></ejs-daterangepicker>`
})
export class AppComponent  { }

Running the application

After completing the configuration required to render a basic DateRangePicker, run the following command to display the output in your default browser.

Copied to clipboard
ng serve

The following example illustrates the output in your browser

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    template: `<ejs-daterangepicker></ejs-daterangepicker>`
})
export class AppComponent {
    constructor() {
    }
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { DateRangePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';


/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        DateRangePickerModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Setting the start and end date

The start and end date in a range can be set by using startDate and endDate properties. To know more about range restriction in DateRangePicker, please refer this page.

The below example demonstrates the DateRangePicker with startDate and endDate.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    template: `<ejs-daterangepicker placeholder='Select a range' [startDate]='start' [endDate]='end'></ejs-daterangepicker>`
})
export class AppComponent {
    public month: number = new Date().getMonth();
    public fullYear: number = new Date().getFullYear();
    public start: Date = new Date(this.fullYear, this.month - 1 , 7);
    public end: Date = new Date(this.fullYear, this.month, 25);
    constructor() {
    }
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { DateRangePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';


/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        DateRangePickerModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

See Also