Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pick date range in angular material 5.0.0 with datepicker?

I am using the latest Angular Material 5.0.0-rc0 in my Angular 5 app. I am trying to select a range of dates with the datepicker provided with Angular material, but I couldn't find any documentation regarding that.

All I could work with it is to select a startDate or set the minDate and maxDate. Here's the HTML code for that

<mat-form-field>
  <input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker startView="year" [startAt]="startDate"></mat-datepicker>
</mat-form-field>

and TS code

import {Component} from '@angular/core';

@Component({
  selector: 'datepicker-start-view-example',
  templateUrl: 'datepicker-start-view-example.html',
  styleUrls: ['datepicker-start-view-example.css'],
})
export class DatepickerStartViewExample {
  minDate = new Date(2000, 0, 1);
  maxDate = new Date(2020, 0, 1);
}

This code helps me select one date in the range of those min and max dates but doesn't allow to select a range of them. Though using a third-party library may solve this problem, but its gonna have a different UI, which is going to be different from my current app's UI. Please help me solve this issue.

like image 844
starlight Avatar asked Nov 08 '17 10:11

starlight


People also ask

How do I set a datepicker range?

To set date range of one month from current date, we will use 'M' option to "+1" for maxDate and for minDate "0" So the current date becomes the value of minDate. See below jQuery code. $(document). ready(function(){ $("#txtDate").

How can use datepicker in angular materials?

mat-datepicker example Add a template reference variable mat-datepicker element. Connect mat-datepicker to input element via [matDatepicker] property. Finally add date picker toggle button to display or hide calender popup by using mat-datepicker-toggle element.

Does angular material have time picker?

The angular material design doesn't have a time picker component but we can use the above two libraries and it provides a handy multifunctional material design time picker for Angular 6.0 and above.


7 Answers

Recommend to check out Saturn Material range Datepicker. Have a look also at their demo page. It is a full Material UX with built-in support for your existing Material theme.

You can install it with npm install saturn-datepicker. See their Github page for full integration instructions.

Markup looks like this:

  <mat-form-field>
    <input matInput
        placeholder="Choose a date"
        [satDatepicker]="picker"
        [value]="date">
    <sat-datepicker #picker [rangeMode]="true"></sat-datepicker>
    <sat-datepicker-toggle matSuffix [for]="picker"></sat-datepicker-toggle>
  </mat-form-field>

And here is what it ends up looking like on the page:

Screenshot of the Material date range picker

like image 77
FirstVertex Avatar answered Sep 23 '22 10:09

FirstVertex


There's currently no possible way to select a range of dates, although there's an issue requesting so.

like image 37
Edric Avatar answered Sep 24 '22 10:09

Edric


In the mean time you could just have two date pickers. One for the start date and one for the end date. Something like this:

<mat-form-field> //start date datepicker
  <input matInput [min]="minDate" [max]="maxDate" [formControl]="startDate" [matDatepicker]="picker" placeholder="Choose a start date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker startView="year" [startAt]="minDate"></mat-datepicker>
</mat-form-field>

<mat-form-field> //end date datepicker
  <input matInput [min]="startDate" [max]="maxDate" [formControl]="endDate" [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker startView="year" [startAt]="startDate"></mat-datepicker>
</mat-form-field>

You could then have some where if they change the endDate to something before the startDate it shows them an error or resets the startDate

like image 35
mariogarcia Avatar answered Sep 23 '22 10:09

mariogarcia


It is even not possible in Angular Material 7.0.0. There are different plugins to do so. I came up with ngx-aaa-datepicker. It worked for me.

Preview

You can check this out in NPM

like image 21
Abdun Nahid Avatar answered Sep 21 '22 10:09

Abdun Nahid


There's an open issue at the github page.

https://github.com/angular/material2/issues/4763

In Angular-Blog, there's a note for this too (look at the end).

https://blog.angular.io/taking-advantage-of-the-angular-material-datepicker-237e80fa14b3

like image 30
Oswald Avatar answered Sep 24 '22 10:09

Oswald


The Angular version of the Angular UI Bootstrap library has a date range selector, you can check out that. I am using it in my projects.

https://ng-bootstrap.github.io/#/components/datepicker/examples

like image 34
anurag619 Avatar answered Sep 23 '22 10:09

anurag619


Now available in Angular Material v10.0.1 or more

https://material.angular.io/components/datepicker/overview#date-range-selection

like image 33
Edmilson Lani Avatar answered Sep 22 '22 10:09

Edmilson Lani