jQuery UI API - Datepicker Widget
Category
Usage
**Description:** Selects a date from a popup or inline calendar.
**Version added:** 1.0
The jQuery UI Datepicker is a highly configurable plugin that adds date-picking functionality to a page. You can customize the date format and language, restrict the range of selectable dates, and add buttons and other navigation options.
By default, the datepicker opens in a small overlay when the associated text field gains focus. For an inline calendar, simply attach the datepicker to a div or span.
Keyboard interaction
When the datepicker is open, the following keyboard commands are available:
- PAGE UP: Moves to the previous month.
- PAGE DOWN: Moves to the next month.
- CTRL+PAGE UP: Moves to the previous year.
- CTRL+PAGE DOWN: Moves to the next year.
- CTRL+HOME: Moves to the current month. Opens the datepicker if it is closed.
- CTRL+LEFT: Moves to the previous day.
- CTRL+RIGHT: Moves to the next day.
- CTRL+UP: Moves to the previous week.
- CTRL+DOWN: Moves to the next week.
- ENTER: Selects the focused date.
- CTRL+END: Closes the datepicker and clears the date.
- ESCAPE: Closes the datepicker without making a selection.
Utility functions
$.datepicker.setDefaults( settings )
Changes the default settings for all datepickers.
Use option() method to change the settings of an individual instance.
Code example:
Sets all datepickers to open on focus or when the icon is clicked.
$.datepicker.setDefaults({
showOn: "both",
buttonImageOnly: true,
buttonImage: "calendar.gif",
buttonText: "Calendar"
});
Sets all datepickers to have French text.
$.datepicker.setDefaults( $.datepicker.regional[ "fr" ] );
$.datepicker.formatDate( format, date, settings )
Formats a date into a string value with the specified format.
The format can be a combination of the following:
- d - Day of the month (no leading zero)
- dd - Day of the month (two digits)
- o - Day of the year (no leading zero)
- oo - Day of the year (three digits)
- D - Short name of the day
- DD - Long name of the day
- m - Month of the year (no leading zero)
- mm - Month of the year (two digits)
- M - Short name of the month
- MM - Long name of the month
- y - Year (two digits)
- yy - Year (four digits)
- @ - Unix timestamp (ms since 01/01/1970)
- ! - Windows clock (100ns since 01/01/0001)
- '...' - Text
- '' - Single quote
- Other - Text
There are also some $.datepicker predefined standard date formats:
- ATOM - 'yy-mm-dd' (same as RFC 3339/ISO 8601)
- COOKIE - 'D, dd M yy'
- ISO_8601 - 'yy-mm-dd'
- RFC_822 - 'D, d M y' (see RFC 822)
- RFC_850 - 'DD, dd-M-y' (see RFC 850)
- RFC_1036 - 'D, d M y' (see RFC 1036)
- RFC_1123 - 'D, d M yy' (see RFC 1123)
- RFC_2822 - 'D, d M yy' (see RFC 2822)
- RSS - 'D, d M y' (same as RFC 822)
- TICKS - '!'
- TIMESTAMP - '@'
- W3C - 'yy-mm-dd' (same as ISO 8601)
Code example:
Displays a date in ISO format. Produces "2007-01-26".
$.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) );
Displays a date in the extended French format. Produces "Samedi, Juillet 14, 2007".
$.datepicker.formatDate( "DD, MM d, yy", new Date( 2007, 7 - 1, 14 ), {
dayNamesShort: $.datepicker.regional[ "fr" ].dayNamesShort,
dayNames: $.datepicker.regional[ "fr" ].dayNames,
monthNamesShort: $.datepicker.regional[ "fr" ].monthNamesShort,
monthNames: $.datepicker.regional[ "fr" ].monthNames
});
$.datepicker.parseDate( format, value, settings )
Extracts a date from a string value in a specified format.
The format can be a combination of the following:
- d - Day of the month (no leading zero)
- dd - Day of the month (two digits)
- o - Day of the year (no leading zero)
- oo - Day of the year (three digits)
- D - Short name of the day of the week
- DD - Long name of the day of the week
- m - Month of the year (no leading zero)
- mm - Month of the year (two digits)
- M - Short name of the month
- MM - Long name of the month
- y - Year (two digits)
- yy - Year (four digits)
- @ - Unix timestamp (ms since 01/01/1970)
- ! - Windows clock (100ns since 01/01/0001)
- '...' - Text
- '' - Single quote
- Other - Text
Some exceptions that may be thrown:
- 'Invalid arguments' - Thrown if the format or value is empty.
- 'Missing number at position nn' - Thrown if the format indicates a numeric value that is not found.
- 'Unknown name at position nn' - Thrown if the format indicates a day-of-week or month name that is not found.
- 'Unexpected literal at position nn' - Thrown if the format indicates a literal value that is not found.
- 'Invalid date' - Thrown if the date is invalid, for example '31/02/2007'.
Code example:
Extracts a date in ISO format.
$.datepicker.parseDate( "yy-mm-dd", "2007-01-26" );
Extracts a date in the extended French format.
$.datepicker.parseDate( "DD, MM d, yy", "Samedi, Juillet 14, 2007", {
shortYearCuroff: 20,
dayNamesShort: $.datepicker.regional[ "fr" ].dayNamesShort,
dayNames: $.datepicker.regional[ "fr" ].dayNames,
monthNamesShort: $.datepicker.regional[ "fr" ].monthNamesShort,
monthNames: $.datepicker.regional[ "fr" ].monthNames
});
$.datepicker.iso8601Week( date )
Determines the week number of a given date within the year: 1 to 53.
This function uses the ISO 8601 definition of a week: a week starts on Monday and the first week of a year contains January 4. This means at most three days of the previous year may fall in the first week of the current year, and at most three days of the current year may fall in the last week of the previous year.
This function is an alias of the calculateWeek The default implementation of the option.
Code example:
Finds the week number of a date within the year.
$.datepicker.iso8601Week( new Date( 2007, 1 - 1, 26 ) );
$.datepicker.noWeekends
Sets a function such as beforeShowDay to prevent selecting weekends.
We can in beforeShowDay provide a noWeekends() function in the option to calculate all working days, providing an true/false array of values indicating whether a date is selectable.
Code example:
Sets the DatePicker to make weekends unselectable.
$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends
});
Limitation
The datepicker provides support for localization to suit different languages and date formats. Each localization is contained in a file with the language code appended to the name, for example jquery.ui.datepicker-fr.js. The required localization file must be included after the main datepicker code. Each localization file adds its own settings to the set of available localizations, and all instances automatically apply these settings as defaults.
$.datepicker.regional The property holds an array of localizations, prefixed with language codes; the default prefix is "", indicating English. Each entry is an object with the following properties:closeText、prevText、nextText、currentText、monthNames、monthNamesShort、dayNames、dayNamesShort、dayNamesMin、weekHeader、dateFormat、firstDay、isRTL、showMonthAfterYear and yearSuffix。
You can restore the default localization with the following code:
$.datepicker.setDefaults( $.datepicker.regional[ "" ] );
You can override the datepicker for a specific locale with the following code:
$( selector ).datepicker( $.datepicker.regional[ "fr" ] );
Theming
The Datepicker widget uses the jQuery UI CSS Framework to define its look-and-feel styles. If you need to use datepicker-specific styles, the following CSS class names are available:
ui-datepicker: the outer container of the datepicker. If the datepicker is inline, this element will additionally have aui-datepicker-inlineclass. If set toisRTLoption, the element will additionally have aui-datepicker-rtlclass。ui-datepicker-header: The header container of the datepicker.ui-datepicker-prev: The control used to select the previous month.ui-datepicker-next: The control used to select the next month.ui-datepicker-title: The title container of the datepicker that holds the month and year.ui-datepicker-month: The text display of the month; if thechangeMonthoption is set, it displays.ui-datepicker-year: The text display of the year; if thechangeYearoption is set, it displays.
ui-datepicker-calendar: The table containing the calendar.ui-datepicker-week-end: Cells for weekend days. : Cells containing weekend days.ui-datepicker-other-month: Cells for days that occur in a month but are not days of the current month.ui-datepicker-unselectable: Cells that cannot be selected by the user.ui-datepicker-current-day: The cell of the selected date.ui-datepicker-today: The cell of the current day's date.
ui-datepicker-buttonpaneWhen theshowButtonPaneloption is set, a button pane (buttonpane) is used.ui-datepicker-current: The button used to select the current day's date.
ui-datepicker-prev: The control used to select the previous month.ui-datepicker-next: The control used to select the next month.ui-datepicker-title: The title container of the datepicker that holds the month and year.ui-datepicker-month: The text display of the month; if thechangeMonthoption is set, it displays.ui-datepicker-year: The text display of the year; if thechangeYearoption is set, it displays.
ui-datepicker-month: The text display of the month; if thechangeMonthoption is set, it displays.ui-datepicker-year: The text display of the year; if thechangeYearoption is set, it displays.
ui-datepicker-calendar: The table containing the calendar.ui-datepicker-week-end: Cells for weekend days. : Cells containing weekend days.ui-datepicker-other-month: Cells for days that occur in a month but are not days of the current month.ui-datepicker-unselectable: Cells that cannot be selected by the user.ui-datepicker-current-day: The cell of the selected date.ui-datepicker-today: The cell of the current day's date.
ui-datepicker-buttonpaneWhen theshowButtonPaneloption is set, a button pane (buttonpane) is used.ui-datepicker-current: The button used to select the current day's date.
If both the numberOfMonths option is used to display multiple months, some additional classes are used:
ui-datepicker-multi: The outermost container of a multiple-month datepicker. Depending on the number of months to display, this element additionally has aui-datepicker-multi-2、ui-datepicker-multi-3orui-datepicker-multi-4class name.ui-datepicker-group: An individual picker within a group. Depending on its position in the group, this element additionally has aui-datepicker-group-first、ui-datepicker-group-middleorui-datepicker-group-lastclass name.
Depends on
- UI Core
- Effects Core(optional; when used with the
showAnimoption)
Additional Notes
- This widget requires some functional CSS; otherwise it will not work. If you create a custom theme, use the widget-specific CSS file as a starting point.
- This widget manipulates the element's value programmatically, so changing the element's value does not fire a native
changeevents. - Creating a datepicker on it is not supported, because it would cause a UI conflict with the native picker.
Not supported on
Example
A simple jQuery UI datepicker.
日期选择器部件(Datepicker Widget)演示