Angular 2+ datepicker

Angular 2+ datepicker


Published at - Aug 13, 2021

Date Picker

This is a configurable date-picker build for Angular applications.
Supports latest (Angular 10) version. For older versions please checkout CHANGELOG.md file.

DEMO

npm version Build Status Package Quality

Screenshots

Day:

Imgur

DayTime:

Imgur

Month:

Imgur

Time:

Imgur

Installation:

  1. Download from npm:
    npm install ng2-date-picker --save
  2. Import the DpDatePickerModule module:
    import {DpDatePickerModule} from 'ng2-date-picker';

    1. Add DpDatePickerModule to your module imports:
 @NgModule({ ... imports: [ ... DpDatePickerModule ] })
 ```  

## Contributors  

This project exists thanks to all the people who contribute. [[Contribute]](CONTRIBUTING.md).  
<a href="graphs/contributors"><img src="https://opencollective.com/angular-datepicker/contributors.svg?width=890" /></a>  


## Backers  

Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/angular-datepicker#backer)]  
<a href="https://opencollective.com/angular-datepicker#backers" target="_blank" rel="noopener"><img src="https://opencollective.com/angular-datepicker/backers.svg?width=890"></a>  


## Usage  
Put the dp-date-picker component wherever you need it.  


### Attributes (Input):   
| Name                 | Type                                | Default            | Applies To                | Description                                                                                                                                                                                                                                        |  
|----------------------|:-----------------------------------:|:------------------:|:-------------------------:|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
| mode                 | `"day"\|"month"\|"time"\|"daytime"` | `"day"`            | All                       | The mode of the calender which will be displayed in the picker                                                                                                                                                                                     |  
| displayDate          | `Moment\|String`                    | current date       | `day\|month\|daytime`     | Indicates on what date to open the calendar on                                                                                                                                                                                                        |  
| disabled             | `Boolean`                           | `false`            | All                       | If set to true the input would be disabled                                                                                                                                                                                                         |  
| placeholder          | `String`                            | `""`               | All                       | The date-picker input placeholder                                                                                                                                                                                                                  |  
| required             | `Boolean`                           | `undefined`        | All                       | This is a validation rule, if there won't be any selected date then the containing form will be invalid.                                                                                                                                           |  
| minDate              | `Moment\|String`                    | `undefined`        | `day\|month\|daytime`     | This is a validation rule, if the selected date will be before `minDate` the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.                                            |  
| maxDate              | `Moment\|String`                    | `undefined`        | `day\|month\|daytime`     | This is a validation rule, if the selected date will be after `maxDate` the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.                                             |  
| minTime              | `Moment\|String`                    | `undefined`        | `time`                    | This is a validation rule, if the selected date will be before `minTime` the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.                                            |  
| maxTime              | `Moment\|String`                    | `undefined`        | `time`                    | This is a validation rule, if the selected date will be after `maxTime` the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.                                             |  
| theme                | `String`                            | `""`               | All                       | Theme is a class added to the popup container (and inner components) - this will allow styling of the calendar when it's appended to outer element (for example - body). There is a built in theme named dp-material, you can find it in the demo. |  
| config               | `IDatePickerConfig`                 | See Below          | All                       | Configuration object - see description below.                                                                                                                                                                                                      |  

### Attributes (Output):   
| Name                 | Event Arguments                     | Applies To                | Description                                                                                                                                                      |  
|----------------------|:-----------------------------------:|:-------------------------:|------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
| onChange             | `CalendarValue`                     | All Pickers               | This event will be emitted on every valid value change, if you want to receive every value (valid and invalid) please use the native `ngModelChange` output.     |  
| open                 | `undefined`                         | All Pickers               | This event will be emitted when picker is opened.                                                                                                                |  
| close                | `CalendarValue`                     | All Pickers               | This event will be emitted when picker is closed.                                                                                                                |  
| onGoToCurrent        | void                                | All Pickers               | This event will be emitted when click was made on go to current button.                                                                                          |  
| onLeftNav            | void                                | All Pickers               | This event will be emitted when click was made on left navigation button.                                                                                        |  
| onRightNav           | void                                | All Pickers               | This event will be emitted when click was made on right navigation button.                                                                                       |  
| onSelect             | ISelectionEvent                     | All Pickers               | This event will be emitted when a date was selected via click or input change (with appropriate indication)                                                      |  

### Configuration: In order to provide configurations to the date-picker you need to pass it to the `dp-date-picker` component:   

```html  
<dp-date-picker [(ngModel)]="selectedDate" [config]="datePickerConfig"></dp-date-picker>  
</code></pre>

Here are the available configurations:

<table>
<thead>
<tr>
  <th>Name</th>
  <th align="center">Type</th>
  <th align="center">Default</th>
  <th align="center">Applies To</th>
  <th>Description</th>
</tr>
</thead>
<tbody>
<tr>
  <td>appendTo</td>
  <td align="center"><code>String\|HTMLElement</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center">All</td>
  <td>The selector/element to which the calendar popup will append to (this is useful for <code>overflow: hidden</code> container issues). Please note that the <code>appendTo</code> element will be set with position <code>absolute</code> if it has position <code>static</code> (the default position).</td>
</tr>
<tr>
  <td>locale</td>
  <td align="center"><code>String</code></td>
  <td align="center"><a class="wp-editor-md-post-content-link" href="https://momentjs.com/docs/#/i18n/getting-locale/"><code>moment.locale()</code></a></td>
  <td align="center">All</td>
  <td>Localisation of language (see in the demo all supported locales)</td>
</tr>
<tr>
  <td>disableKeypress</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>false</code></td>
  <td align="center">All</td>
  <td>Disables the possibility to change the date value by typing it - changing the date would be possible only from the picker</td>
</tr>
<tr>
  <td>format</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"DD-MM-YYYY"</code></td>
  <td align="center">All</td>
  <td>If ngModel provided as <code>String</code> the format is required, this format also will be used as the input format style.</td>
</tr>
<tr>
  <td>onOpenDelay</td>
  <td align="center"><code>Number</code></td>
  <td align="center"><code>0</code></td>
  <td align="center">All</td>
  <td>The delay (in ms) between the date picker focusing and the date-picker popup apparel</td>
</tr>
<tr>
  <td>drops</td>
  <td align="center"><code>'up'\|'down'</code></td>
  <td align="center"><code>undefined</code> down if possible</td>
  <td align="center">All</td>
  <td>Whether the picker appears below or above the input element.</td>
</tr>
<tr>
  <td>opens</td>
  <td align="center"><code>'right'\|'left'</code></td>
  <td align="center"><code>undefined</code> right if possible</td>
  <td align="center">All</td>
  <td>Whether the picker appears aligned to the left or to the right the input element.</td>
</tr>
<tr>
  <td>closeOnSelect</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center"><code>day\|month</code></td>
  <td>If set to <code>true</code> will close the date-picker after choosing a date from the calender, otherwise, won't.</td>
</tr>
<tr>
  <td>openOnClick</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>If set to <code>false</code> will not open the date-picker when input gets clicked, otherwise, will.</td>
</tr>
<tr>
  <td>openOnFocus</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>If set to <code>false</code> will not open the date-picker when input gets focused, otherwise, will.</td>
</tr>
<tr>
  <td>closeOnSelectDelay</td>
  <td align="center"><code>Number</code></td>
  <td align="center"><code>100</code></td>
  <td align="center"><code>day\|month</code></td>
  <td>The delay (in ms) between the date selection and the date-picker collapse</td>
</tr>
<tr>
  <td>allowMultiSelect</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|month</code></td>
  <td>If set to <code>true</code> will allow for choosing multiple dates. <code>false</code> will force a single selection. If <code>undefined</code>, the picker will attempt to guess based on the type of the input value.</td>
</tr>
<tr>
  <td>dayBtnFormat</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>DD</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>The day format of the day button in the calendar.</td>
</tr>
<tr>
  <td>dayBtnFormatter</td>
  <td align="center"><code>(Moment) => String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>The formatter (callback function) of the day button in the calendar.</td>
</tr>
<tr>
  <td>dayBtnCssClassCallback</td>
  <td align="center"><code>(Moment) => String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>Callback for adding custom CSS classes to the day button in the calendar. Can contain multiple classes, just seperate with a space.</td>
</tr>
<tr>
  <td>enableMonthSelector</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>Whether to enable/disable the selection of a moth granularity picker.</td>
</tr>
<tr>
  <td>firstDayOfWeek</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"su"</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>The first day of the calendar's week. Should be one of: <code>"su", "mo", "tu", "we", "th", "fr", "sa"</code></td>
</tr>
<tr>
  <td>isDayDisabledCallback</td>
  <td align="center"><code>(Moment) => boolean</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>Callback which should indicate if specific day is disabled.</td>
</tr>
<tr>
  <td>monthFormat</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"MMM-YYYY"</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>The date format of the day calendar, the one that seen above the calendar days. Will be overwritten if <code>monthFormatter</code> provided.</td>
</tr>
<tr>
  <td>monthFormatter</td>
  <td align="center"><code>(Moment) => String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>The date formatter (callback function) of the day calendar, the one that seen above the calendar days.</td>
</tr>
<tr>
  <td>showNearMonthDays</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>Whether to show/hide next and previous month days.</td>
</tr>
<tr>
  <td>showWeekNumbers</td>
  <td align="center"><code>Boolean</code></td>
  <td align="center"><code>false</code></td>
  <td align="center"><code>day\|daytime</code></td>
  <td>Whether to show/hide the week number of each week (iso week number).</td>
</tr>
<tr>
  <td>isMonthDisabledCallback</td>
  <td align="center"><code>(Moment) => boolean</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>Callback which should indicate if specific month is disabled (month selector).</td>
</tr>
<tr>
  <td>max</td>
  <td align="center"><code>Moment\|String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>Disables all dates (on the date-picker) that are set to after the <code>max</code>, note that if invalid date would be set by the input then the date picker value would be the max date but the input will show the user typed date.</td>
</tr>
<tr>
  <td>min</td>
  <td align="center"><code>Moment\|String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>Disables all dates (on the date-picker) that are set to before the <code>min</code>, note that if invalid date would be set by the input then the date picker value would be the min date but the input will show the user typed date.</td>
</tr>
<tr>
  <td>monthBtnFormat</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>DD</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>The month format of the month button in the calendar.</td>
</tr>
<tr>
  <td>monthBtnFormatter</td>
  <td align="center"><code>(Moment) => String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>The formatter (callback function) of the month button in the calendar.</td>
</tr>
<tr>
  <td>monthBtnCssClassCallback</td>
  <td align="center"><code>(Moment) => String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>Callback for adding custom CSS classes to the month button in the calendar. Can contain multiple classes, just seperate with a space.</td>
</tr>
<tr>
  <td>yearFormat</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"YYYY"</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>The date format of the month calendar, the one that seen above the calendar months. Will be overwritten if <code>yearFormatter</code> provided. (available when <code>enableMonthSelector</code> is set to <code>true</code>).</td>
</tr>
<tr>
  <td>yearFormatter</td>
  <td align="center"><code>(Moment) => String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>The date formatter (callback function) of the month calendar, the one that seen above the calendar months. (available when <code>enableMonthSelector</code> is set to <code>true</code>).</td>
</tr>
<tr>
  <td>hours12Format</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"hh"</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The hours format of the time select when <code>showTwentyFourHours</code> is <code>false</code>.</td>
</tr>
<tr>
  <td>hours24Format</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"HH"</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The hours format of the time select when <code>showTwentyFourHours</code> is <code>true</code>.</td>
</tr>
<tr>
  <td>maxTime</td>
  <td align="center"><code>Moment\|String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>Disables arrow buttons on the time select that would make the time after the <code>maxTime</code>.</td>
</tr>
<tr>
  <td>meridiemFormat</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"A"</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The AM/PM format of the time select when <code>showTwentyFourHours</code> is <code>false</code>.</td>
</tr>
<tr>
  <td>minTime</td>
  <td align="center"><code>Moment\|String</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>Disables arrow buttons on the time select that would make the time before the <code>minTime</code>.</td>
</tr>
<tr>
  <td>minutesFormat</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"mm"</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The minutes format of the time select.</td>
</tr>
<tr>
  <td>minutesInterval</td>
  <td align="center"><code>number</code></td>
  <td align="center"><code>1</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The number of minutes that will be added/subtracted when clicking up/down arrows on the time select.</td>
</tr>
<tr>
  <td>secondsFormat</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>"ss"</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The seconds format of the time select.</td>
</tr>
<tr>
  <td>secondsInterval</td>
  <td align="center"><code>number</code></td>
  <td align="center"><code>1</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The number of seconds that will be added/subtracted when clicking up/down arrows on the time select.</td>
</tr>
<tr>
  <td>showSeconds</td>
  <td align="center"><code>boolean</code></td>
  <td align="center"><code>false</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>If set to <code>true</code> will show seconds in the time select, otherwise, won't.</td>
</tr>
<tr>
  <td>showTwentyFourHours</td>
  <td align="center"><code>boolean</code></td>
  <td align="center"><code>false</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>If set to <code>true</code> will show hours in 24 hour format. <code>false</code> will show hours in 12 hours format and append AM/PM to the end of the time select.</td>
</tr>
<tr>
  <td>timeSeparator</td>
  <td align="center"><code>String</code></td>
  <td align="center"><code>":"</code></td>
  <td align="center"><code>daytime\|time</code></td>
  <td>The separator that will be placed between hours and minutes and between minutes and seconds on the time select.</td>
</tr>
<tr>
  <td>showMultipleYearsNavigation</td>
  <td align="center"><code>boolean</code></td>
  <td align="center"><code>false</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>If set to <code>true</code> will show buttons to navigate by multiple years (10 by default)</td>
</tr>
<tr>
  <td>multipleYearsNavigateBy</td>
  <td align="center"><code>number</code></td>
  <td align="center"><code>10</code></td>
  <td align="center"><code>day\|month\|daytime</code></td>
  <td>Number of years to navigate when showMultipleYearsNavigation is <code>true</code></td>
</tr>
<tr>
  <td>returnedValueType</td>
  <td align="center"><code>ECalendarValue</code></td>
  <td align="center"><code>Moment</code></td>
  <td align="center">All</td>
  <td>The returned value type (<code>Moment</code>, <code>Moment[]</code>, <code>string</code>, <code>string[]</code></td>
</tr>
<tr>
  <td>unSelectOnClick</td>
  <td align="center"><code>boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center"><code>day\|month</code></td>
  <td>Will allow disallow/unselect to selected date by clicking on the selected date</td>
</tr>
<tr>
  <td>inputElementContainer</td>
  <td align="center"><code>string\|HTMLElement</code></td>
  <td align="center"><code>undefined</code></td>
  <td align="center">All</td>
  <td>Will place picker popup relative to the provided elemenr (if string provided will used as a selector)</td>
</tr>
<tr>
  <td>showGoToCurrent</td>
  <td align="center"><code>boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center">All</td>
  <td>Show/Hides the go to current button on the calendars navigation</td>
</tr>
<tr>
  <td>hideOnOutsideClick</td>
  <td align="center"><code>boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center">All</td>
  <td>Show/Hides the picker popup after click outside of the component</td>
</tr>
<tr>
  <td>closeOnEnter</td>
  <td align="center"><code>boolean</code></td>
  <td align="center"><code>true</code></td>
  <td align="center">All</td>
  <td>Hides the picker popup after enter button keypress</td>
</tr>
</tbody>
</table>

<h3>API:</h3>

In order to use the date-picker api user the <code>@ViewChild</code> annotation in the date-picker containing component class, take at the example below:<br />
Container component:<br />

```ts import {Component, ViewChild} from '@angular/core';  
import {DatePickerComponent} from 'ng2-date-picker';  


@Component({
selector: 'my-container',
template: <div> <h1>Container</h1> <dp-date-picker #dayPicker></dp-date-picker> <button (click)="open()"></button> <button (click)="close()"></button></div>
});
class MyContainer {
@ViewChild('dayPicker') datePicker: DatePickerComponent;
open() { this.datePicker.api.open(); }
close() { this.datePicker.api.close(); }} ``` If you want to use API with Directive - you can do it by using #dateDirectivePicker, like below:
import {Component, ViewChild} from '@angular/core';  
import {DatePickerDirective} from 'ng2-date-picker';  

@Component({  
selector: 'my-container',  
template: `  
<div>  
 <input #dateDirectivePicker="dpDayPicker"> <button (click)="close()"></button></div>  
`  
});  
class MyContainer {  
 @ViewChild('dateDirectivePicker') datePickerDirective: DatePickerDirective;     close() {  
 this.datePickerDirective.api.close(); }} ```  

Here is the list of APIs:    

| Name                 | Signature                          | Description                      |  
|----------------------|:----------------------------------:|----------------------------------|  
| open                 | `() => void`                       | Opens the date picker            |  
| close                | `() => void`                       | Closes the date picker           |  
| moveCalendarTo       | `(to: Moment \| String) => void`   | Moves calendar to specific date  |  

## Inline - Day Calendar  

You can use the `<dp-day-calendar>` component to display the calendar widget without an associated input box.  

i.e.  
```html  
<dp-day-calendar [(ngModel)]="selectedDate" [config]="config"></dp-day-calendar>  

Attributes:

Name Type Default Description
required Boolean undefined This is a validation rule, if there won't be any selected date then the containing form will be invalid.
minDate Moment\|String undefined This is a validation rule, if the selected date will be before minDate the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
maxDate Moment\|String undefined This is a validation rule, if the selected date will be after maxDate the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
theme String '' Theme is a class added to the popup container (and inner components) - this will allow styling of the calendar when it's appended to outer element (for example - body). There is a built in theme named dp-material, you can find it in the demo.
config IDayCalendarConfig See Below Configuration object - see description below.

Attributes (Output):

Name Event Arguments Description
onSelect IDay This event will be emitted when a day is selected.
onMonthSelect IMonth This event will be emitted when a month is selected.
onNavHeaderBtnClick ECalendarMode This event will be emitted when the mode of the calendar switches form day to month and vise versa.
onGoToCurrent void This event will be emitted when click was made on go to current button.
onLeftNav void This event will be emitted when click was made on left navigation button.
onRightNav void This event will be emitted when click was made on right navigation button.

Configuration: In order to provide configurations to the day-calendar you need to pass it to the dp-day-calendar component:

<dp-day-calendar [(ngModel)]="selectedDate" [config]="config"></dp-day-calendar>  

Here are the available configurations:

Name Type Default Description
format String "DD-MM-YYYY" If ngModel provided as String the format is required, this format also will be used as the input format style.
firstDayOfWeek String "su" The first day of the calendar's week. Should be one of: "su", "mo", "tu", "we", "th", "fr", "sa"
monthFormat String "MMM-YYYY" The date format of the day calendar, the one that seen above the calendar days. Will be overwritten if monthFormatter provided.
monthFormatter (Moment) => String undefined The date formatter (callback function) of the day calendar, the one that seen above the calendar days.
yearFormat String "YYYY" The date format of the month calendar, the one that seen above the calendar months. Will be overwritten if yearFormatter provided. (available when enableMonthSelector is set to true).
yearFormatter (Moment) => String undefined The date formatter (callback function) of the month calendar, the one that seen above the calendar months. (available when enableMonthSelector is set to true).
allowMultiSelect Boolean undefined If set to true will allow for choosing multiple dates. false will force a single selection. If undefined, the picker will attempt to guess based on the type of the input value.
min Moment\|String undefined Disables all dates (on the date-picker) that are set to before the min, note that if invalid date would be set by the input then the date picker value would be the min date but the input will show the user typed date.
max Moment\|String undefined Disables all dates (on the date-picker) that are set to after the max, note that if invalid date would be set by the input then the date picker value would be the max date but the input will show the user typed date.
showNearMonthDays Boolean true Whether to show/hide next and previous month days.
showWeekNumbers Boolean false Whether to show/hide the week number of each week (iso week number).
enableMonthSelector Boolean true Whether to enable/disable the selection of a moth granularity picker.
isDayDisabledCallback (Moment) => boolean undefined Callback which should indicate if specific day is disabled.
isMonthDisabledCallback (Moment) => boolean undefined Callback which should indicate if specific month is disabled (month selector).
dayBtnFormat String DD The day format of the day button in the calendar.
dayBtnFormatter (Moment) => String undefined The formatter (callback function) of the day button in the calendar.
dayBtnCssClassCallback (Moment) => String undefined Callback which can add custom CSS classes to the day button in the calendar. Multiple classes can be returned with a space between them.
monthBtnFormat String DD The month format of the month button in the calendar.
monthBtnFormatter (Moment) => String undefined The formatter (callback function) of the month button in the calendar.
monthBtnCssClassCallback (Moment) => String undefined Callback which can add custom CSS classes to the month button in the calendar. Multiple classes can be returned with a space between them.
showMultipleYearsNavigation boolean false If set to true will show buttons to navigate by multiple years (10 by default)
multipleYearsNavigateBy number 10 Number of years to navigate when showMultipleYearsNavigation is true
numOfMonthRows 1, 2, 3, 4, 6, 12 4 Number of rows to be shown on month calendar

Inline - Month Calendar

You can use the <dp-month-calendar> component to display the calendar widget without an associated input box.
i.e.

<dp-month-calendar [(ngModel)]="selectedDate" [config]="config"></dp-month-calendar>  

Attributes:

Name Type Default Description
required Boolean undefined This is a validation rule, if there won't be any selected date then the containing form will be invalid.
minDate Moment\|String undefined This is a validation rule, if the selected date will be before minDate the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
maxDate Moment\|String undefined This is a validation rule, if the selected date will be after maxDate the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
theme String '' Theme is a class added to the popup container (and inner components) - this will allow styling of the calendar when it's appended to outer element (for example - body). There is a built in theme named dp-material, you can find it in the demo.
config IMonthPickerConfig See Below Configuration object - see description below.

Attributes (Output):

Name Event Arguments Description
onSelect IMonth This event will be emitted when a month is selected.
onNavHeaderBtnClick null This event will be emitted when the mode button, in the navigation section, was clicked.
onGoToCurrent void This event will be emitted when click was made on go to current button.
onLeftNav void This event will be emitted when click was made on left navigation button.
onRightNav void This event will be emitted when click was made on right navigation button.

Configuration: In order to provide configurations to the month-calendar you need to pass it to the dp-month-calendar component:

<dp-month-calendar [(ngModel)]="selectedDate" [config]="config"></dp-month-calendar>  

Here are the available configurations:

Name Type Default Description
format String "DD-MM-YYYY" If ngModel provided as String the format is required, this format also will be used as the input format style.
yearFormat String "YYYY" The date format of the month calendar, the one that seen above the calendar months. Will be overwritten if yearFormatter provided. (available when enableMonthSelector is set to true).
yearFormatter (Moment) => String undefined The date formatter (callback function) of the month calendar, the one that seen above the calendar months. (available when enableMonthSelector is set to true).
allowMultiSelect Boolean undefined If set to true will allow for choosing multiple dates. false will force a single selection. If undefined, the picker will attempt to guess based on the type of the input value.
min Moment\|String undefined Disables all dates (on the date-picker) that are set to before the min, note that if invalid date would be set by the input then the date picker value would be the min date but the input will show the user typed date.
max Moment\|String undefined Disables all dates (on the date-picker) that are set to after the max, note that if invalid date would be set by the input then the date picker value would be the max date but the input will show the user typed date.
isMonthDisabledCallback (Moment) => boolean undefined Callback which should indicate if specific month is disabled (month selector).
monthBtnFormat String DD The month format of the month button in the calendar.
monthBtnFormatter (Moment) => String undefined The formatter (callback function) of the month button in the calendar.
monthBtnCssClassCallback (Moment) => String undefined Callback which can add custom CSS classes to the month button in the calendar. Multiple classes can be returned with a space between them.
showMultipleYearsNavigation boolean false If set to true will show buttons to navigate by multiple years (10 by default)
multipleYearsNavigateBy number 10 Number of years to navigate when showMultipleYearsNavigation is true
numOfMonthRows 1, 2, 3, 4, 6, 12 4 Number of rows to be shown on month calendar

Here is the list of APIs:

Name Signature Description
moveCalendarsBy (current: Moment, amount: number, granularity: moment.unitOfTime.Base) => void Moves calendar by given amount
moveCalendarTo (to: Moment \| String) => void Moves calendar to specific date
toggleCalendarMode (mode: day \| month) => void Changes clander mode day/month

Inline - Time Select

You can use the <dp-time-select> component to display the time select widget without an associated input box.

i.e.

<dp-time-select [(ngModel)]="selectedDate" [config]="config"></dp-time-select>  

Attributes:

Name Type Default Description
required Boolean undefined This is a validation rule, if there won't be any selected date then the containing form will be invalid.
minTime Moment\|String undefined This is a validation rule, if the selected date will be before minTime the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
maxTime Moment\|String undefined This is a validation rule, if the selected date will be after maxTime the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
theme String '' Theme is a class added to the popup container (and inner components) - this will allow styling of the calendar when it's appended to outer element (for example - body). There is a built in theme named dp-material, you can find it in the demo.
config ITimeSelectConfig See Below Configuration object - see description below.

Attributes (Output):

Name Event Arguments Description
onChange IDate This event will be emitted when time is selected.

Configuration: In order to provide configurations to the time-select you need to pass it to the dp-time-select component:

<dp-time-select [(ngModel)]="selectedDate" [config]="config"></dp-time-select>  

Here are the available configurations:

Name Type Default Description
hours12Format String "hh" The hours format of the time select when showTwentyFourHours is false.
hours24Format String "HH" The hours format of the time select when showTwentyFourHours is true.
maxTime Moment\|String undefined Disables arrow buttons on the time select that would make the time after the maxTime.
meridiemFormat String "A" The AM/PM format of the time select when showTwentyFourHours is false.
minTime Moment\|String undefined Disables arrow buttons on the time select that would make the time before the minTime.
minutesFormat String "mm" The minutes format of the time select.
minutesInterval number 1 The number of minutes that will be added/subtracted when clicking up/down arrows on the time select.
secondsFormat String "ss" The seconds format of the time select.
secondsInterval number 1 The number of seconds that will be added/subtracted when clicking up/down arrows on the time select.
showSeconds boolean false If set to true will show seconds in the time select, otherwise, won't.
showTwentyFourHours boolean false If set to true will show hours in 24 hour format. false will show hours in 12 hours format and append AM/PM to the end of the time select.
timeSeparator String ":" The separator that will be placed between hours and minutes and between minutes and seconds on the time select.

Inline - DayTime Calendar

You can use the <dp-day-time-calendar> component to display the calendar widget and time select widget without an associated input box.

i.e.

<dp-day-time-calendar [(ngModel)]="selectedDate" [config]="config"></dp-day-time-calendar>  

Attributes:

Name Type Default Description
required Boolean undefined This is a validation rule, if there won't be any selected date then the containing form will be invalid.
minDate Moment\|String undefined This is a validation rule, if the selected date will be before minDate the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
maxDate Moment\|String undefined This is a validation rule, if the selected date will be after maxDate the containing form will be invalid. Note: if provided as string format configuration should be provided in the config object.
theme String '' Theme is a class added to the popup container (and inner components) - this will allow styling of the calendar when it's appended to outer element (for example - body). There is a built in theme named dp-material, you can find it in the demo.
config IDatePickerConfig See Below Configuration object - see description below.

Attributes (Output):

Name Event Arguments Description
onChange IDate This event will be emitted when time is selected.
onGoToCurrent void This event will be emitted when click was made on go to current button.
onLeftNav void This event will be emitted when click was made on left navigation button.
onRightNav void This event will be emitted when click was made on right navigation button.

Configuration: In order to provide configurations to the day-time-calendar you need to pass it to the dp-day-time-calendar component:

<dp-day-time-calendar [(ngModel)]="selectedDate" [config]="config"></dp-day-time-calendar>  

Here are the available configurations:

Name Type Default Description
format String "DD-MM-YYYY" If ngModel provided as String the format is required, this format also will be used as the input format style.
firstDayOfWeek String "su" The first day of the calendar's week. Should be one of: "su", "mo", "tu", "we", "th", "fr", "sa"
monthFormat String "MMM-YYYY" The date format of the day calendar, the one that seen above the calendar days. Will be overwritten if monthFormatter provided.
monthFormatter (Moment) => String undefined The date formatter (callback function) of the day calendar, the one that seen above the calendar days.
yearFormat String "YYYY" The date format of the month calendar, the one that seen above the calendar months. Will be overwritten if yearFormatter provided. (available when enableMonthSelector is set to true).
yearFormatter (Moment) => String undefined The date formatter (callback function) of the month calendar, the one that seen above the calendar months. (available when enableMonthSelector is set to true).
allowMultiSelect Boolean undefined If set to true will allow for choosing multiple dates. false will force a single selection. If undefined, the picker will attempt to guess based on the type of the input value.
min Moment\|String undefined Disables all dates (on the date-picker) that are set to before the min, note that if invalid date would be set by the input then the date picker value would be the min date but the input will show the user typed date.
max Moment\|String undefined Disables all dates (on the date-picker) that are set to after the max, note that if invalid date would be set by the input then the date picker value would be the max date but the input will show the user typed date.
showNearMonthDays Boolean true Whether to show/hide next and previous month days.
showWeekNumbers Boolean false Whether to show/hide the week number of each week (iso week number).
enableMonthSelector Boolean true Whether to enable/disable the selection of a moth granularity picker.
isDayDisabledCallback (Moment) => boolean undefined Callback which should indicate if specific day is disabled.
isMonthDisabledCallback (Moment) => boolean undefined Callback which should indicate if specific month is disabled (month selector).
dayBtnFormat String DD The day format of the day button in the calendar.
dayBtnFormatter (Moment) => String undefined The formatter (callback function) of the day button in the calendar.
dayBtnCssClassCallback (Moment) => String undefined Callback which can add custom CSS classes to the day button in the calendar. Multiple classes can be returned with a space between them.
monthBtnFormat String DD The month format of the month button in the calendar.
monthBtnFormatter (Moment) => String undefined The formatter (callback function) of the month button in the calendar.
monthBtnCssClassCallback (Moment) => String undefined Callback which can add custom CSS classes to the month button in the calendar. Multiple classes can be returned with a space between them.
hours12Format String "hh" The hours format of the time select when showTwentyFourHours is false.
hours24Format String "HH" The hours format of the time select when showTwentyFourHours is true.
meridiemFormat String "A" The AM/PM format of the time select when showTwentyFourHours is false.
minutesFormat String "mm" The minutes format of the time select.
minutesInterval Number 1 The number of minutes that will be added/subtracted when clicking up/down arrows on the time select.
secondsFormat String "ss" The seconds format of the time select.
secondsInterval Number 1 The number of seconds that will be added/subtracted when clicking up/down arrows on the time select.
showSeconds boolean false If set to true will show seconds in the time select, otherwise, won't.
showTwentyFourHours boolean false If set to true will show hours in 24 hour format. false will show hours in 12 hours format and append AM/PM to the end of the time select.
timeSeparator String ":" The separator that will be placed between hours and minutes and between minutes and seconds on the time select.
showMultipleYearsNavigation boolean false If set to true will show buttons to navigate by multiple years (10 by default).
multipleYearsNavigateBy Number 10 Number of years to navigate when showMultipleYearsNavigation is true.
hideInputContainer Boolean false Will hide the input element of any picker if set to true.
weekDayFormat String ddd The format of the weekday name.
weekDayFormatter (Number) => String undefined You can customize the value of any weekday with this configuration. The parameter of the callback will start with 0 as Sunday and so on.
numOfMonthRows 1, 2, 3, 4, 6, 12 4 Number of rows to be shown on month calendar

Directive

You can use the [dpDayPicker] directive to attach the picker to any component with an ngModel or a FormControl (using reactive forms).

i.e.

<input name="someName" [(ngModel)]="selectedDate" [dpDayPicker]="config" />  

or using reactive forms:

<input name="someName" formControlName="selectedDate" [dpDayPicker]="config" />  
<!-- OR -->  
<input name="someName" [formControl]="selectedDateFormControl" [dpDayPicker]="config" />  

or with @angular/material:

<md-input-container>  
 <input mdInput name="someName" [(ngModel)]="selectedDate" [dpDayPicker]="config" theme="dp-material" attachTo=".mat-input-wrapper" /></md-input-container>  

Attributes:

Name Type Default Description
mode 'day'\|'month'\|'time'\|'daytime' 'day' the type of the calender which will be displayed in the picker
attachTo ElementRef\|String undefined the element used to position the picker. If attachTo is a String, it is used as a css selector to match any parent of the directive's host component. If attachTo is undefined, the host component itself is used.
theme String '' Theme is a class added to the popup container (and inner components) - this will allow styling of the calendar when it's appended to outer element (for example - body). There is a built in theme named dp-material, you can find it in the demo.
config IDatePickerDirectiveConfig See Below Configuration object - see description below.

Configuration: In order to provide configurations to the date-picker you need to pass it to the [dpDayPicker] directive:

<input [(ngModel)]="selectedDate" [dpDayPicker]="datePickerConfig" />  

The IDatePickerDirectiveConfig is identical to IDatePickerConfig above except that it lacks the showGoToCurrent property.

Here is the list of APIs:

Name Signature Description
moveCalendarTo (to: Moment \| String) => void Moves calendar to specific date

Compatibility

Internet Explorer 10:

Internet explorer 10 doesn't support hidden attribut, but you can use a css rule on data-hidden to hide date picker components:

[data-hidden="true"] {  
 display: none !important;}  

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]














About author

Harendra
Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.



Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile