Introduction

In this article we are going to learn the use of Ngx-Bootstrap DateRange Picker and Date Picker in Angular 8.
Ngx-Bootstrap has released a package of open-source tools which is native Angular directives for Bootstrap 3 and 4. It contains all core components powered by Angular. In this article we will learn about DatePicker component which is a cool feature of Ngx-bootstrap.
Prerequisites
  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed
Step 1
Let's create a new Angular project using the following NPM command,
  1. ng new datepicker
Step 2
Now, let's create a new component by using the following command,
  1. ng g c ngx-bootstrap-datepicker
Step 3
Install ngx-bootstrap from npm by using the folowing command.
  1. npm install ngx-bootstrap --save
Or,
  1. ng add ngx-bootstrap --component datepicker
Step 4
Now let's add bootstrap styles in our index.html file .
For Bootstrap 3,
  1. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
For Bootstrap 4,
  1. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
Step 5
Let add the template in our ngx-bootstrap-datepicker.component.html.
  1. <div class="card">
  2. <div class="card-body pb-0">
  3. <h4 style="text-align: center;">Example of Date Picker and Date Range Picker</h4>
  4. <div class="row">
  5. <div class="col-12 col-md-12">
  6. <div class="card">
  7. <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top nav-kc">
  8. <div class="col-md-6">
  9. <div class="input-group">
  10. <input type="text" name="daterange" autocomplete="off" bsDaterangepicker
  11. [bsConfig]="{ rangeInputFormat : 'MMMM Do YYYY', dateInputFormat: 'MMMM Do YYYY', showWeekNumbers: false }"
  12. id="DateOfBirth" placeholder="Date Range" (bsValueChange)="dateRangeCreated($event)"
  13. class="form-control" />
  14. </div>
  15. </div>
  16. <div class="col-md-3">
  17. <div class="input-group">
  18. <input type="text" name="daterange" autocomplete="off" bsDatepicker
  19. [bsConfig]="{ dateInputFormat: 'MMMM Do YYYY', showWeekNumbers: false }" id="DateOfBirth"
  20. placeholder="Date" (bsValueChange)="dateCreated($event)" class="form-control" />
  21. </div>
  22. </div>
  23. </nav>
  24. <div class="card-body position-relative">
  25. <div class="table-responsive cnstr-record product-tbl">
  26. <table class="table table-bordered heading-hvr">
  27. <thead>
  28. <tr>
  29. <th>ArtNo.</th>
  30. <th>Brand</th>
  31. <th>
  32. Price/Unit</th>
  33. <th>Provider</th>
  34. <th>P. Art. N</th>
  35. <th>S. A/C</th>
  36. <th>Buy A/C</th>
  37. <th>Created Date</th>
  38. </tr>
  39. </thead>
  40. <tbody *ngFor="let product of products; let i = index">
  41. <tr>
  42. <td align="center">{{product.ArtNo}}</td>
  43. <td>{{product.Brand}}</td>
  44. <td>{{product.Price }}</td>
  45. <td>{{product.Provider}}</td>
  46. <td>{{product.ProviderArtNo}}</td>
  47. <td>{{product.SalesAccount}}</td>
  48. <td>{{product.BuyAccount}}</td>
  49. <td>{{product.CreatedDate}}</td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
In bsConfig method we can format the date and time. showWeekNumbers is a boolean value which is true by default and displays the number of the week .
Also we can select the min date. max date, etc.
  1. <input type="text" name="daterange" autocomplete="off" bsDatepicker
  2. [bsConfig]="{ dateInputFormat: 'MMMM Do YYYY', showWeekNumbers: false }" id="DateOfBirth"
  3. placeholder="Date" (bsValueChange)="dateCreated($event)" class="form-control" />
Step 6
Now, open the ngx-bootstrap-datepicker.component.ts file and add the following code in this file.
  1. import { Component, OnInit } from '@angular/core';
  2. import { DatePickerService } from '../datepicker.service';
  3. @Component({
  4. selector: 'app-ngx-bootstrap-datepicker',
  5. templateUrl: './ngx-bootstrap-datepicker.component.html',
  6. styleUrls: ['./ngx-bootstrap-datepicker.component.css']
  7. })
  8. export class NgxBootstrapDatepickerComponent implements OnInit {
  9. products = [];
  10. tempproducts=[];
  11. constructor(private datepickerService: DatePickerService) { }
  12. ngOnInit() {
  13. this.getProducts();
  14. }
  15. getProducts() {
  16. this.products = this.datepickerService.getAllProducts();
  17. this.tempproducts = this.datepickerService.getAllProducts();
  18. }
  19. dateCreated($event){
  20. this.products = this.tempproducts;
  21. this.products = this.products.filter(x => x.CreatedDate == $event.toJSON().split('T')[0]);
  22. }
  23. dateRangeCreated($event) {
  24. this.products = this.tempproducts;
  25. let startDate = $event[0].toJSON().split('T')[0];
  26. let endDate = $event[1].toJSON().split('T')[0];
  27. this.products = this.products.filter(
  28. m => new Date(m.CreatedDate) >= new Date(startDate) && new Date(m.CreatedDate) <= new Date(endDate)
  29. );
  30. }
  31. }
Step 7
Create a service dataservice using the following NPM command
  1. ng g service data
Now, open the data.service.ts file and add the following code,
  1. import { Injectable } from '@angular/core';
  2. @Injectable()
  3. export class DataService {
  4. employees: any[];
  5. getAllProducts() {
  6. return this.employees = [
  7. {
  8. ProductId: 1,
  9. ArtNo: "100",
  10. Provider: "OppoProvider",
  11. ProviderArtNo: "1Yu",
  12. Brand: "Oppo",
  13. Price: 7810.23,
  14. BuyAccount: "123",
  15. SalesAccount: "321",
  16. CreatedDate: "2020-04-17"
  17. },
  18. {
  19. ProductId: 1,
  20. ArtNo: "101",
  21. Provider: "OppoProvider",
  22. ProviderArtNo: "1Yu",
  23. Brand: "Oppo",
  24. Price: 2310.23,
  25. BuyAccount: "123",
  26. SalesAccount: "321",
  27. CreatedDate: "2020-04-15"
  28. },
  29. {
  30. ProductId: 1,
  31. ArtNo: "102",
  32. Provider: "OppoProvider",
  33. ProviderArtNo: "1Yu",
  34. Brand: "Oppo",
  35. Price: 7810.23,
  36. BuyAccount: "123",
  37. SalesAccount: "321",
  38. CreatedDate: "2020-04-11"
  39. },
  40. {
  41. ProductId: 1,
  42. ArtNo: "103",
  43. Provider: "OppoProvider",
  44. ProviderArtNo: "1Yu",
  45. Brand: "Oppo",
  46. Price: 5810.23,
  47. BuyAccount: "123",
  48. SalesAccount: "321",
  49. CreatedDate: "2020-03-21"
  50. },
  51. {
  52. ProductId: 1,
  53. ArtNo: "104",
  54. Provider: "OppoProvider",
  55. ProviderArtNo: "1Yu",
  56. Brand: "Oppo",
  57. Price: 4770.23,
  58. BuyAccount: "143",
  59. SalesAccount: "211",
  60. CreatedDate: "2020-03-01"
  61. },
  62. ];
  63. }
  64. }
Step 8
Now, open the app.component.html file and add the following code in the file,
  1. <app-ngx-bootstrap-datepicker></app-ngx-bootstrap-datepicker>
Step 9
Let's open app.module.ts file and add the following code in the file,
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { FormsModule} from '@angular/forms';
  4. import { AppComponent } from './app.component';
  5. import { BsDatepickerModule } from 'ngx-bootstrap/datepicker';
  6. import { NgxBootstrapDatepickerComponent } from './ngx-bootstrap-datepicker/ngx-bootstrap-datepicker.component';
  7. @NgModule({
  8. declarations: [
  9. AppComponent,
  10. NgxBootstrapDatepickerComponent
  11. ],
  12. imports: [
  13. BrowserModule,
  14. FormsModule,
  15. BsDatepickerModule.forRoot()
  16. ],
  17. bootstrap: [AppComponent]
  18. })
  19. export class AppModule { }
Now it's time for the output,
Ngx-Bootstrap DateRange Picker And Date Picker
Ngx-Bootstrap DateRange Picker And Date Picker
Date range picker is where we can select a range of datess. After selecting date one event will fire which is an array,
Ngx-Bootstrap DateRange Picker And Date Picker
The below image shows that it will fetch only those data which comes in the range from April 1 to April 14.
Ngx-Bootstrap DateRange Picker And Date Picker
The below image shows the normal ngx-bootstrap datepicker.
Ngx-Bootstrap DateRange Picker And Date Picker
Ngx-Bootstrap DateRange Picker And Date Picker

Conclusion

I hope you have enjoyed this article as much as I have enjoyed writing and coding the examples.
Please let me know how to improve it.