How To Add DateRange Picker In Angular Application

Introduction

 
In this article, we will learn how to add date range picker In Angular application.
 
Prerequisites
  • Basic Knowledge of Angular 2 or higher
  • Visual Studio Code
  • Node and NPM installed
Let's create an Angular project by using the following command.
  1. ng new rangesliderdemo  
Open this project in Visual Studio Code and install bootstrap by using following command.
  1. npm install bootstrap --save     
Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line. 
  1. @import '~bootstrap/dist/css/bootstrap.min.css';   
Now install ngx-daterange library using the following command. 
  1. npm install ngx-daterange --save  
Now create a new component using the following command,
  1. ng g c sliderdemo  
Now open sliderdemo.component.ts file and add the following code,
  1. import { Component, OnInit, ViewChild } from '@angular/core';  
  2. import { DateRangePickerComponent, IDateRange, IDateRangePickerOptions } from 'ngx-daterange';  
  3. import * as moment from 'moment';  
  4. @Component({  
  5.   selector: 'app-sliderdemo',  
  6.   templateUrl: './sliderdemo.component.html',  
  7.   styleUrls: ['./sliderdemo.component.css']  
  8. })  
  9. export class SliderdemoComponent implements OnInit {  
  10.   constructor() { }  
  11.   
  12.   @ViewChild('dateRangePicker', { statictrue })  
  13.   dateRangePicker: DateRangePickerComponent;  
  14.   firstFieldEmittedValue: IDateRange;  
  15.   firstFieldOptions: IDateRangePickerOptions = {  
  16.     format: 'MM/DD/YYYY',  
  17.     minDate: moment().subtract(10, 'years'),  
  18.     maxDate: moment().add(3, 'years'),  
  19.     preDefinedRanges: [  
  20.       {  
  21.         name: 'Last Week',  
  22.         value: {  
  23.           start: moment().subtract(1, 'week').startOf('week'),  
  24.           end: moment().subtract(1, 'week').endOf('week')  
  25.         }  
  26.       },  
  27.       {  
  28.         name: 'Two Weeks Ago',  
  29.         value: {  
  30.           start: moment().subtract(2, 'week').startOf('week'),  
  31.           end: moment().subtract(2, 'week').endOf('week')  
  32.         }  
  33.       },  
  34.       {  
  35.         name: 'Three Weeks Ago',  
  36.         value: {  
  37.           start: moment().subtract(3, 'week').startOf('week'),  
  38.           end: moment().subtract(3, 'week').endOf('week')  
  39.         }  
  40.       },  
  41.       {  
  42.         name: 'Last Month',  
  43.         value: {  
  44.           start: moment().subtract(1, 'month').startOf('month'),  
  45.           end: moment().subtract(1, 'month').endOf('month')  
  46.         }  
  47.       },  
  48.         
  49.       
  50.     ]  
  51.   }  
  52.   secondFieldOptions: IDateRangePickerOptions = {  
  53.     autoApply: false,  
  54.     clickOutsideAllowed: false,  
  55.     format: 'MM/DD/YYYY',  
  56.     icons: 'font-awesome',  
  57.     minDate: moment().subtract(10, 'years'),  
  58.     maxDate: moment().add(1, 'year'),  
  59.   }  
  60.   ngOnInit(): void {  
  61.      
  62.   }  
  63. }  
Now open sliderdemo.component.html file and add the following code,
  1. <div class="row" style="margin-top:10px;margin-bottom: 10px;">          
  2.     <div class="col-sm-12 btn btn-success">          
  3.       How to Create Date Range Picker in Angular application    
  4.     </div>          
  5.   </div>      
  6. <div class="col-sm-6 form-group">      
  7.   <date-range-picker [options]="firstFieldOptions"  [ngModel]="'myDateRange'">  
  8.   </date-range-picker>   
  9. </div>    
Now open sliderdemo.component.css file and add the following css classes.
  1.  .dateRangePicker-wrapper .dateRangePicker .form-inputs>.col {  
  2.     min-width: 270px;  
  3.     padding: .5rem 15px;  
  4.     background-color: #28a745;  
  5. }  
  6.  .dateRangePicker-wrapper .dateRangePicker:before {  
  7.  border-bottom: 0px solid #6e777c !important;  
  8.    
  9. }  
Now open app.module.ts file and add the following code,
  1. import { NgModule } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { HttpClientModule } from '@angular/common/http'  
  4. import { AppComponent } from './app.component';  
  5. import { SliderdemoComponent } from './sliderdemo/sliderdemo.component';  
  6. import { FormsModule } from '@angular/forms';  
  7. import { NgxDateRangeModule } from 'ngx-daterange';  
  8. @NgModule({  
  9.   declarations: [  
  10.     AppComponent,  
  11.     SliderdemoComponent  
  12.       
  13.   ],  
  14.   imports: [  
  15.     BrowserModule,,HttpClientModule,NgxDateRangeModule,FormsModule  
  16.   ],  
  17.   providers: [],  
  18.   bootstrap: [AppComponent]  
  19. })  
  20. export class AppModule { }  
Now open app.component.html file and add the following code,
  1. <app-sliderdemo></app-sliderdemo>  
Now, run the application using the 'npm start' command and check the result.
 
 
Select start date and end date
 

Summary

 
In this article, we learned how to add daterange picker In Angular application.


Similar Articles