CalendarCalendar is an input component to select a date.

Calendar - 图1

Documentation

Import

  1. import {CalendarModule} from 'primeng/calendar';
  2.  

Getting Started

Two-way value binding is defined using the standard ngModel directive referencing to a Date property.

  1. <p-calendar [(ngModel)]="value"></p-calendar>
  2.  
  1. export class MyModel {
  2. value: Date;
  3. }
  4.  

Model Driven Forms

Calendar can be used in a model driven form as well.

  1. <p-calendar formControlName="date"></p-calendar>
  2.  

Popup and Inline

Calendar is displayed in a popup by default and inline property needs to be enabled for inline mode.

  1. <p-calendar [(ngModel)]="value" [inline]="true"></p-calendar>
  2.  

Selection Mode

By default calendar allows selecting one date and multiple dates can be selected by setting selectionMode to multiple. In this case calendar updates the value with an array of dates where optionally number of selectable dates can be restricted with maxDateCount property. Third alternative is the range mode that allows selecting a range based on an array of two values where first value is the start date and second value is the end date.

DateFormat

Default date format is mm/dd/yy, to customize this use dateFormat property or define it at locale settings. Note that standalone property overrides the value in locale settings.

  1. <p-calendar [(ngModel)]="dateValue" dateFormat="dd.mm.yy"></p-calendar>
  2.  

Following options can be a part of the format.

  • d - day of month (no leading zero)
  • dd - day of month (two digit)
  • o - day of the year (no leading zeros)
  • oo - day of the year (three digit)
  • D - day name short
  • DD - day name long
  • m - month of year (no leading zero)
  • mm - month of year (two digit)
  • M - month name short
  • MM - month name long
  • y - year (two digit)
  • yy - year (four digit)
  • @ - Unix timestamp (ms since 01/01/1970)
  • ! - Windows ticks (100ns since 01/01/0001)
  • '…' - literal text
  • '' - single quote
  • anything else - literal text

Time

TimePicker is enabled with showTime property and 24 (default) or 12 hour mode is configured using hourFormat option.

  1. <p-calendar [(ngModel)]="value" showTime="true" hourFormat="12"></p-calendar>
  2. <p-calendar [(ngModel)]="value" showTime="true" hourFormat="24"></p-calendar>
  3.  

Date Restriction

To disable entering dates manually, set readonlyInput to true and to restrict selectable dates use minDate and maxDate options.

  1. <p-calendar [(ngModel)]="dateValue" [minDate]="minDateValue" [maxDate]="maxDateValue" [readonlyInput]="true"></p-calendar>
  2.  

Disable specific dates and/or days

To disable specific dates or days, set readonlyInput to true and to restrict selectable dates use disabledDates and/or disabledDays options.

  1. <p-calendar [(ngModel)]="dateValue" [disabledDates]="invalidDates" [disabledDays]="[0,6]" [readonlyInput]="true"></p-calendar>
  2.  

Button Bar

Button bar displays today and clear buttons and enabled using showButtonBar property.

  1. <p-calendar [(ngModel)]="dateValue" showButtonBar="true"></p-calendar>
  2.  

Multiple Months

Displaying multiple months is enabled by setting numberOfMonths property to a value greater than 1.

  1. <p-calendar [(ngModel)]="dateValue" [numberOfMonths]="3"></p-calendar>
  2.  

Localization

Localization for different languages and formats is defined by binding the locale settings object to the locale property. Following is the default values for English.

  1. <p-calendar [(ngModel)]="dateValue" [locale]="en"></p-calendar>
  2.  
  1. export class MyModel {
  2. en: any;
  3. ngOnInit() {
  4. this.en = {
  5. firstDayOfWeek: 0,
  6. dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  7. dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  8. dayNamesMin: ["Su","Mo","Tu","We","Th","Fr","Sa"],
  9. monthNames: [ "January","February","March","April","May","June","July","August","September","October","November","December" ],
  10. monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
  11. today: 'Today',
  12. clear: 'Clear',
  13. dateFormat: 'mm/dd/yy',
  14. weekHeader: 'Wk'
  15. };
  16. }
  17. }
  18.  

Custom Content

Calendar UI accepts custom content using p-header and p-footer components.

  1. <p-calendar [(ngModel)]="dateValue">
  2. <p-header>Header</p-header>
  3. <p-footer>Footer</p-footer>
  4. </p-calendar>
  5.  

In addition, cell contents can be templated using an ng-template with a pTemplate directive whose value is "date". This is a handy feature to highlight specific dates. Note that the implicit variable passed to the template is not a date instance but a metadata object to represent a Date with "day", "month" and "year" properties. Example below changes the background color of dates between 10th and 21st of each month.

  1. <p-calendar [(ngModel)]="date10">
  2. <ng-template pTemplate="date" let-date>
  3. <span [ngStyle]="{backgroundColor: (date.day < 21 && date.day > 10) ? '#7cc67c' : 'inherit'}" style="border-radius:50%">{{date.day}}</span>
  4. </ng-template>
  5. </p-calendar>
  6.  

Month Picker

Month picker is used to select month and year only without the date, set view mode as "month" to activate month picker.

  1. <p-calendar [(ngModel)]="dateValue" view="month" dateFormat="mm/yy" [yearNavigator]="true" yearRange="2000:2030"></p-calendar>
  2.  

Touch UI

Touch UI mode displays the calendar overlay at the center of the screen as optimized for touch devices. When using Touch UI mode you will typically want to set readonlyInput to prevent keyboard popup for mobile users.

  1. <p-calendar [(ngModel)]="dateValue" [touchUI]="true" [readonlyInput]="true"></p-calendar>
  2.  

Animation Configuration

Transition of the open and hide animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.

  1. <p-calendar [(ngModel)]="dateValue" [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'"></p-calendar>
  2.  

Properties

NameTypeDefaultDescription
defaultDateDatenullSet the date to highlight on first opening if the field is blank.
selectionModestringsingleDefines the quantity of the selection, valid values are "single", "multiple" and "range".
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
inputStylestringnullInline style of the input field.
inputStyleClassstringnullStyle class of the input field.
inputIdstringnullIdentifier of the focus input to match a label defined for the component.
namestringnullName of the input element.
placeholderstringnullPlaceholder text for the input.
disabledbooleanfalseWhen specified, disables the component.
dateFormatstringmm/dd/yyFormat of the date which can also be defined at locale settings.
inlinebooleanfalseWhen enabled, displays the calendar as inline. Default is false for popup mode.
showOtherMonthsbooleantrueWhether to display dates in other months (non-selectable) at the start or end of the current month. To make these days selectable use the selectOtherMonths option.
selectOtherMonthsbooleanfalseWhether days in other months shown before or after the current month are selectable. This only applies if the showOtherMonths option is set to true.
showIconbooleanfalseWhen enabled, displays a button with icon next to input.
showOnFocusbooleantrueWhen disabled, datepicker will not be visible with input focus.
showWeekbooleanfalseWhen enabled, calendar will show week numbers.
iconstringpi pi-calendarIcon of the calendar button.
appendToanynullTarget element to attach the overlay, valid values are "body" or a local ng-template variable of another element.
readonlyInputbooleannullWhen specified, prevents entering the date manually with keyboard.
shortYearCutoffstring+10The cutoff year for determining the century for a date.
minDateDatenullThe minimum selectable date.
maxDateDatenullThe maximum selectable date.
disabledDatesArray<Date>nullArray with dates that should be disabled (not selectable).
disabledDaysArray<number>nullArray with weekday numbers that should be disabled (not selectable).
monthNavigatorbooleanfalseWhether the month should be rendered as a dropdown instead of text.
yearNavigatorbooleanfalseWhether the year should be rendered as a dropdown instead of text.
yearRangestringnullThe range of years displayed in the year drop-down in (nnnn:nnnn) format such as (2000:2020).
showTimebooleanfalseWhether to display timepicker.
hourFormatstring24Specifies 12 or 24 hour format.
localeobjectnullAn object having regional configuration properties for the calendar.
timeOnlybooleanfalseWhether to display timepicker only.
timeSeparatorstring:Separator of time selector.
dataTypestringdateType of the value to write back to ngModel, default is date and alternative is string.
requiredbooleanfalseWhen present, it specifies that an input field must be filled out before submitting the form.
tabindexnumbernullIndex of the element in tabbing order.
showSecondsbooleanfalseWhether to show the seconds in time picker.
stepHournumber1Hours to change per step.
stepMinutenumber1Minutes to change per step.
stepSecondnumber1Seconds to change per step.
maxDateCountnumbernullMaximum number of selectable dates in multiple mode.
showButtonBarbooleanfalseWhether to display today and clear buttons at the footer
todayButtonStyleClassstringui-secondary-buttonStyle class of the today button.
clearButtonStyleClassstringui-secondary-buttonStyle class of the clear button.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
panelStyleClassstringnullStyle class of the datetimepicker container element.
panelStyleobjectnullInline style of the datetimepicker container element.
keepInvalidbooleanfalseKeep invalid value when input blur.
hideOnDateTimeSelectbooleanfalseWhether to hide the overlay on date selection when showTime is enabled.
numberOfMonthsnumber1Number of months to display.
viewstringdateType of view to display, valid values are "date" for datepicker and "month" for month picker.
touchUIbooleanfalseWhen enabled, calendar overlay is displayed as optimized for touch devices.
showTransitionOptionsstring225ms ease-outTransition options of the show animation.
hideTransitionOptionsstring195ms ease-inTransition options of the hide animation.

Events

NameParametersDescription
onSelectvalue: Selected value Callback to invoke when a date is selected. Note that this event is not called when the value is entered from the input manually.
onBlurevent: Blur event Callback to invoke on blur of input field.
onFocusevent: Focus event Callback to invoke on focus of input field.
onCloseevent: Close event Callback to invoke when datepicker panel is closed.
onInputevent: Input event Callback to invoke when input field is being typed.
onTodayClickevent: Click event Callback to invoke when today button is clicked.
onClearClickevent: Click event Callback to invoke when clear button is clicked.
onMonthChangeevent.month: New month event.year: New year Callback to invoke when a month is changed using the navigators.
onYearChangeevent.month: New month event.year: New year Callback to invoke when a year is changed using the navigators.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-calendarMain container element
ui-calendar-w-btnMain container element when button is enabled.
ui-calendar-timeonlyMain container element in time picker only mode.
ui-inputtextInput element
ui-datepickerDatepicker element
ui-datepicker-inlineDatepicker element in inline mode
ui-datepicker-monthpickerDatepicker element in month view.
ui-datepicker-touch-uiDatepicker element in touch ui mode.
ui-datepicker-calendarTable containing dates of a month.
ui-datepicker-current-dayCell of selected date.
ui-datepicker-todayCell of today's date.

Dependencies

None.

Source

View on GitHub

  1. <div class="ui-g ui-fluid">
  2. <div class="ui-g-12 ui-md-4">
  3. <h3>Basic</h3>
  4. <p-calendar [(ngModel)]="date1"></p-calendar>
  5. </div>
  6. <div class="ui-g-12 ui-md-4">
  7. <h3>Spanish</h3>
  8. <p-calendar [(ngModel)]="date2" [locale]="es" dateFormat="dd/mm/yy"></p-calendar>
  9. </div>
  10. <div class="ui-g-12 ui-md-4">
  11. <h3>Icon</h3>
  12. <p-calendar [(ngModel)]="date3" [showIcon]="true"></p-calendar>
  13. </div>
  14. <div class="ui-g-12 ui-md-4">
  15. <h3>Min-Max</h3>
  16. <p-calendar [(ngModel)]="date4" [minDate]="minDate" [maxDate]="maxDate" [readonlyInput]="true"></p-calendar>
  17. </div>
  18. <div class="ui-g-12 ui-md-4">
  19. <h3>Disable Days</h3>
  20. <p-calendar [(ngModel)]="date5" [disabledDates]="invalidDates" [disabledDays]="[0,6]" [readonlyInput]="true"></p-calendar>
  21. </div>
  22. <div class="ui-g-12 ui-md-4">
  23. <h3>Navigators</h3>
  24. <p-calendar [(ngModel)]="date6" [monthNavigator]="true" [yearNavigator]="true" yearRange="2000:2030"></p-calendar>
  25. </div>
  26. <div class="ui-g-12 ui-md-4">
  27. <h3>Time</h3>
  28. <p-calendar [(ngModel)]="date7" [showTime]="true"></p-calendar>
  29. </div>
  30. <div class="ui-g-12 ui-md-4">
  31. <h3>Time Only </h3>
  32. <p-calendar [(ngModel)]="date8" [timeOnly]="true"></p-calendar>
  33. </div>
  34. <div class="ui-g-12 ui-md-4">
  35. <h3>Multiple </h3>
  36. <p-calendar [(ngModel)]="dates" selectionMode="multiple" [readonlyInput]="true"></p-calendar>
  37. </div>
  38. <div class="ui-g-12 ui-md-4">
  39. <h3>Range</h3>
  40. <p-calendar [(ngModel)]="rangeDates" selectionMode="range" [readonlyInput]="true"></p-calendar>
  41. </div>
  42. <div class="ui-g-12 ui-md-4">
  43. <h3>Button Bar</h3>
  44. <p-calendar [(ngModel)]="date9" showButtonBar="true"></p-calendar>
  45. </div>
  46. <div class="ui-g-12 ui-md-4">
  47. <h3>Date Template</h3>
  48. <p-calendar [(ngModel)]="date11">
  49. <ng-template pTemplate="date" let-date>
  50. <span [ngStyle]="{backgroundColor: (date.day < 21 && date.day > 10) ? '#81C784' : 'inherit'}" style="border-radius:50%;padding: .25em;width: 1.75em; height:1.75em; display:block;">{{date.day}}</span>
  51. </ng-template>
  52. </p-calendar>
  53. </div>
  54. <div class="ui-g-12 ui-md-4">
  55. <h3>Multiple Months</h3>
  56. <p-calendar [(ngModel)]="date11" [numberOfMonths]="3"></p-calendar>
  57. </div>
  58. <div class="ui-g-12 ui-md-4">
  59. <h3>Month Picker</h3>
  60. <p-calendar [(ngModel)]="date12" view="month" dateFormat="mm/yy" [yearNavigator]="true" yearRange="2000:2030"></p-calendar>
  61. </div>
  62. <div class="ui-g-12 ui-md-4">
  63. <h3>Touch UI</h3>
  64. <p-calendar [(ngModel)]="date13" [touchUI]="true"></p-calendar>
  65. </div>
  66. </div>
  67. <h3>Inline</h3>
  68. <p-calendar [(ngModel)]="date14" [inline]="true" [showWeek]="true"></p-calendar>
  69.  
  1. export class CalendarDemo {
  2. date1: Date;
  3. date2: Date;
  4. date3: Date;
  5. date4: Date;
  6. date5: Date;
  7. date6: Date;
  8. date7: Date;
  9. date8: Date;
  10. date9: Date;
  11. date10: Date;
  12. date11: Date;
  13. date12: Date;
  14. date13: Date;
  15. dates: Date[];
  16. rangeDates: Date[];
  17. minDate: Date;
  18. maxDate: Date;
  19. es: any;
  20. invalidDates: Array<Date>
  21. ngOnInit() {
  22. this.es = {
  23. firstDayOfWeek: 1,
  24. dayNames: [ "domingo","lunes","martes","miércoles","jueves","viernes","sábado" ],
  25. dayNamesShort: [ "dom","lun","mar","mié","jue","vie","sáb" ],
  26. dayNamesMin: [ "D","L","M","X","J","V","S" ],
  27. monthNames: [ "enero","febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre" ],
  28. monthNamesShort: [ "ene","feb","mar","abr","may","jun","jul","ago","sep","oct","nov","dic" ],
  29. today: 'Hoy',
  30. clear: 'Borrar'
  31. }
  32. this.tr = {
  33. firstDayOfWeek : 1
  34. }
  35. let today = new Date();
  36. let month = today.getMonth();
  37. let year = today.getFullYear();
  38. let prevMonth = (month === 0) ? 11 : month -1;
  39. let prevYear = (prevMonth === 11) ? year - 1 : year;
  40. let nextMonth = (month === 11) ? 0 : month + 1;
  41. let nextYear = (nextMonth === 0) ? year + 1 : year;
  42. this.minDate = new Date();
  43. this.minDate.setMonth(prevMonth);
  44. this.minDate.setFullYear(prevYear);
  45. this.maxDate = new Date();
  46. this.maxDate.setMonth(nextMonth);
  47. this.maxDate.setFullYear(nextYear);
  48. let invalidDate = new Date();
  49. invalidDate.setDate(today.getDate() - 1);
  50. this.invalidDates = [today,invalidDate];
  51. }
  52. }
  53.