Articles in this section
Category / Section

How to create Angular DateTimePicker component?

3 mins read

 A quick start project that helps you to create an Angular 7 DateTimePicker component with a minimal code configuration.

Angular 7 DateTimePicker

The following section explains the steps required to create a simple Angular 7 DateTimePicker component.

Pre-requisites

Make sure that you have the compatible versions of Angular in your machine before starting to work on this project.

Introduction

The Angular 7 DateTimePicker used in this project is created from the Syncfusion ej2-angular-calendars package. You can simply define it as <ejs-datetimepicker> within the template.

Dependencies

Before starting with this project, the Angular 7 DateTimePicker requires to add the Syncfusion ej2-angular-calendars package from npmjs, which are distributed in npm as @syncfusion scoped packages.

Creating Angular Project

To create the Angular project using the Angular CLI tool, follow the given steps.

Install Angular CLI 7 using following command.

npm install @angular/cli@7.0.4 -g

 

Note:

If you would like to follow and run the application in Angular6 or Angular5 or Angular4, you need to replace the CLI command version number with the corresponding angular version number.

 

npm install -g @angular/cli@<CLI VERSION>

 

Now, create a new Angular project by using the command ng new and navigate to that folder.

ng new <project name>
cd <project name>

 

Install the ej2-angular-calendars package through the npm install command.

npm install @syncfusion/ej2-angular-calendars --save

 

Adding Angular 7 DateTimePicker

You can add the Angular 7 DateTimePicker component by using ejs-datetimepicker directive and the attributes within the tag allows you to define other functionalities.

Import the DateTimePicker module into the Angular application (app.module.ts) from the ej2-angular-calendars package.

import {BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { DateTimePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';
 
@NgModule({ 
    imports: [ BrowserModule, DateTimePickerModule ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
 
export class AppModule {}

 

Define the Angular DateTimePicker code within the app.component.html file mapped against the templateUrl option in app.component.ts file.

Here, the DateTimePicker component rendered with the min and max property.

[app.component.ts]

import {Component} from '@angular/core';
 
@Component ({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
 
export class AppComponent {
    public minDate: Date = new Date ("05/07/2017 2:00 AM");
    public maxDate: Date = new Date ("05/27/2017 11:00 AM");
    public dateValue: Date = new Date ("05/16/2017 5:00 AM");
    constructor () {}
}

 

[app.component.html]

<ejs-datetimepicker id='datetimepicker' placeholder='Select a date' [value]='dateValue' [min]='minDate' [max]='maxDate'></ejs-datetimepicker>

 

Refer to the CDN link of CSS reference within the index.html file.

[index.html]

<link href="https://cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-calendars/styles/material.css" rel="stylesheet" />

 

Run the application with the command ng serve and have a DateTimePicker with min and max date will be displayed on the browser as shown below.

                                                datetimepicker min&max

DateTime Format

DateTime format is a way of representing the date value in different string format in the textbox.

By default, the DateTimePicker’s format is based on the culture. You can also set the own custom format by using the format property.

Note:

Once the datetime format property has been defined it will be common for all cultures

Here, DateTimePicker component rendered with the custom format yyyy-MM-dd HH:mm.

[app.component.ts]

import {Component} from '@angular/core';
 
@Component ({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
 
export class AppComponent {
    public dateValue: Date = new Date ("05/16/2017 3:00 AM");
    constructor () {}
}

 

[app.component.html]

<ejs-datetimepicker id='datetimepicker' placeholder='Select a datetime ' [value]= 'dateValue' format ='yyyy.MM.dd HH:mm'></ejs-datetimepicker>

 

Run the application with the command ng serve and have a DateTimePicker with custom format will be displayed on the browser as shown below.

                                         datetimepicker format

Also, you can download and run the sample from this GitHub Repository. For more information about DateTimePicker functionalities, refer UG Documentation, API Reference , Feature Tour and Samples section.


Conclusion

I hope you enjoyed learning how to create an Angular 7 Date TimePicker component.

You can refer to our Angular DateTimePicker feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our ASP.NET Core PDF and other ASP.NET Core components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied