How to create an Angular 7 DatePicker component?
A quick start project that helps you to create an Angular 7 DatePicker component with a minimal code configuration.
Angular 7 DatePicker
The following section explains the steps required to create a simple Angular 7 DatePicker 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 7+
- Angular CLI
- TypeScript 2.6+
- Visual studio code for editor
Introduction
The Angular 7 DatePicker used in this project is created from the Syncfusion ej2-angular-calendars package. You can simply define it as <ejs-datepicker> within the template.
Dependencies
Before starting with this project, the Angular 7 DatePicker 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
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 DatePicker
You can add the Angular 7 DatePicker component by using ejs-datepicker directive and the attributes within the tag allows you to define other functionalities.
Import the DatePicker 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 { DatePickerModule } from '@syncfusion/ej2-angular-calendars'; import { AppCompnent } from './app.component'; @NgModule({ imports: [ BrowserModule, DatePickerModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule {}
Define the Angular DatePicker code within the app.component.html file mapped against the templateUrl option in app.component.ts file.
Here, the DatePicker 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"); public maxDate: Date = new Date ("05/27/2017"); public dateValue: Date = new Date ("05/16/2017"); constructor () {} }
[app.component.html]
<ejs-datepicker id='datepicker' placeholder='Select a date' [value]='dateValue' [min]='minDate' [max]='maxDate'></ejs-datepicker>
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-calendars/styles/material.css" rel="stylesheet" />
Run the application with the command ng serve and have a DatePicker with min and max date will be displayed on the browser as shown below.
Date Format
Date format is a way of representing the date value in different string format in the textbox.
By default, the DatePicker’s format is based on the culture. You can also set the own custom format by using the format property.
Once the date format property has been defined it will be common for all cultures
Here, DatePicker component rendered with the custom format yyyy-MM-dd.
[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"); constructor () {} }
[app.component.html]
<ejs-datepicker id='datepicker' placeholder='Select a date' [value]= 'dateValue' format ='yyyy-MM-dd'></ejs-datepicker>
Run the application with the command ng serve and have a DatePicker with custom format will be displayed on the browser as shown below.
Also, you can download and run the sample from this GitHub Repository. For more information about DatePicker functionalities, refer UG Documentation, API Reference and Samples section.
Conclusion
I hope you enjoyed learning about how to create an Angular 7 DatePicker component.
You can
refer to our Angular DatePicker feature tour page to know about its other groundbreaking
feature representations and documentation, and how to quickly get
started for configuration specifications.
You can also explore our Angular DatePicker example to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!