How to get started easily with Syncfusion Angular 11 Datetime Picker?
A quick start project that helps you to create an Angular 11 DateTimePicker component with a minimal code configuration.
The following section explains the steps required to create a simple Angular 11 DateTimePicker component.
Pre-requisites
Make sure that you have the compatible versions of Angular in your machine before starting to work on this project.
- Node.js (latest version)
- Angular 11
- Angular CLI
- Typescript 4+
- Visual Studio Code for editor
Introduction
The Angular 11 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 11 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 11 using following command.
npm install -g @angular/cli@11.2.3
If you would like to follow and run the application in Angular9 or earlier, 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 angular11-app cd angular11-app
Install the ej2-angular-calendars package through the npm install command.
npm install @syncfusion/ej2-angular-calendars --save
Adding Angular 11 DateTimePicker
You can add the Angular 11 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.
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.
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.
Refer to our documentation and online samples for more features. If you have any queries, please let us know in comments below. You can also contact us through our Support forum or Direct-Trac. We are happy to assist you!