Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery UI API - Datepicker Widget

jQuery UI API - Datepicker Widget

Category

Widgets

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:

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:

There are also some $.datepicker predefined standard date formats:

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:

Some exceptions that may be thrown:

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-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 the [`changeMonth`](#option-changeMonth) option is set, it displays a `` element. - `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-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-group`: An individual picker within a group. Depending on its position in the group, this element additionally has a `ui-datepicker-group-first`, `ui-datepicker-group-middle`, or `ui-datepicker-group-last` class name.

Depends on

Additional Notes

Example

A simple jQuery UI datepicker.









  

  日期选择器部件(Datepicker Widget)演示