Search results

Getting started with Angular DateTimePicker component

08 Dec 2022 / 4 minutes to read

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

Dependencies

Install the below required dependency package in order to use the DateTimePicker 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-datetimepicker

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-datetimepicker --style=scss

Navigate to the created project folder.

Copied to clipboard
cd syncfusion-angular-datetimepicker

Installing Syncfusion DateTimePicker package

Syncfusion packages are distributed in npm as @syncfusion scoped packages. You can get all the Angular Syncfusion package from npm link.

Currently, Syncfusion provides two types of package structures for Angular components,

  1. Ivy library distribution package format
  2. Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.

Ivy library distribution package

Syncfusion Angular packages(>=20.2.36) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.

Add @syncfusion/ej2-angular-calendars package to the application.

Copied to clipboard
npm install @syncfusion/ej2-angular-calendars --save

Angular compatibility compiled package(ngcc)

For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion Angular components. To download the ngcc package use the below.

Add @syncfusion/ej2-angular-calendars@ngcc package to the application.

Copied to clipboard
npm install @syncfusion/ej2-angular-calendars@ngcc --save

To mention the ngcc package in the package.json file, add the suffix -ngcc with the package version as below.

Copied to clipboard
@syncfusion/ej2-angular-calendars:"20.2.38-ngcc"

Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.

Registering DateTimePicker module

Import DateTimePicker 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 DateTimePickerModule for the DateTimePicker component
import { DateTimePickerModule } from "@syncfusion/ej2-angular-calendars";
import { AppComponent } from "./app.component";

@NgModule({
  //declaration of DateTimePickerModule into NgModule
  imports: [BrowserModule, DateTimePickerModule],
  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 DateTimePicker component

Modify the template in [src/app/app.component.ts] file to render the Angular DateTime Picker component. Add the Angular DateTimePicker by using <ejs-datetimepicker> selector in template section of the app.component.ts file.

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

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

Running the application

After completing the configuration required to render a basic DateTimePicker, 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-datetimepicker></ejs-datetimepicker>`
})
export class AppComponent {
  constructor() {}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { DateTimePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';


/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        DateTimePickerModule,
        FormsModule
    ],
    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 min and max

The minimum and maximum date time can be defined with the help of min and max property. The following example demonstrates to set the min and max on initializing the DateTimePicker. To know more about range restriction in Angular DateTime Picker, please refer this page.

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

@Component({
  selector: 'app-root',
  template: `<ejs-datetimepicker [min]='minDate' [max]='maxDate'></ejs-datetimepicker>`
})
export class AppComponent {
  public month: number = new Date().getMonth();
  public fullYear: number = new Date().getFullYear();
  public minDate: Date = new Date(this.fullYear, this.month , 22, 12);
  public maxDate: Date = new Date(this.fullYear, this.month, 25, 17);
  constructor() {}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { DateTimePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';


/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        DateTimePickerModule,
        FormsModule
    ],
    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);

If the value of min or max properties changed through code behind, then you have to update the value property to set within the range.

N> You can refer to our Angular DateTime Picker feature tour page for its groundbreaking feature representations. You can also explore our Angular DateTime Picker example that shows how to render the DateTime Picker in Angular.

See Also