Datepicker Widgetversion added: 1.0
Description: Select a date from a popup or inline calendar
The jQuery UI Datepicker is a highly configurable plugin that adds datepicker functionality to your pages. You can customize the date format and language, restrict the selectable date ranges and add in buttons and other navigation options easily.
By default, the datepicker calendar 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
While the datepicker is open, the following key commands are available:
- PAGE UP: Move to the previous month.
- PAGE DOWN: Move to the next month.
- CTRL+PAGE UP: Move to the previous year.
- CTRL+PAGE DOWN: Move to the next year.
- CTRL+HOME: Move to the current month. Open the datepicker if closed.
- CTRL+LEFT: Move to the previous day.
- CTRL+RIGHT: Move to the next day.
- CTRL+UP: Move to the previous week.
- CTRL+DOWN: Move the next week.
- ENTER: Select the focused date.
- CTRL+END: Close the datepicker and erase the date.
- ESCAPE: Close the datepicker without selection.
Utility functions
$.datepicker.setDefaults( settings )
Change the default settings for all date pickers.
Use the option()
method to change settings for individual instances.
Set all date pickers to open on focus or a click on an icon.
1
2
3
4
5
6
|
|
Set all date pickers to have French text.
1
|
|
$.datepicker.formatDate( format, date, settings )
Format a date into a string value with a specified format.
The format can be combinations of the following:
- 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
There are also a number of predefined standard date formats available from $.datepicker
:
- 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)
Display the date in ISO format. Produces "2007-01-26".
1
|
|
Display the date in expanded French format. Produces "Samedi, Juillet 14, 2007".
1
2
3
4
5
6
|
|
$.datepicker.parseDate( format, value, settings )
Extract a date from a string value with a specified format.
The format can be combinations of the following:
- d - day of month (no leading zero)
- dd - day of month (two digit)
- o - day of year (no leading zeros)
- oo - day of 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
A number of exceptions may be thrown:
- 'Invalid arguments' if either format or value is null
- 'Missing number at position nn' if format indicated a numeric value that is not then found
- 'Unknown name at position nn' if format indicated day or month name that is not then found
- 'Unexpected literal at position nn' if format indicated a literal value that is not then found
- 'Invalid date' if the date is invalid, such as '31/02/2007'
Extract a date in ISO format.
1
|
|
Extract a date in expanded French format.
1
2
3
4
5
6
7
|
|
$.datepicker.iso8601Week( date )
Determine the week of the year for a given date: 1 to 53.
This function uses the ISO 8601 definition of a week: weeks start on a Monday and the first week of the year contains January 4. This means that up to three days from the previous year may be included in the of first week of the current year, and that up to three days from the current year may be included in the last week of the previous year.
This function is the default implementation for the calculateWeek
option.
Find the week of the year for a date.
1
|
|
$.datepicker.noWeekends
Set as beforeShowDay function to prevent selection of weekends.
We can provide the noWeekends()
function into the beforeShowDay
option which will calculate all the weekdays and provide an array of true
/false
values indicating whether a date is selectable.
Set the DatePicker so no weekend is selectable
1
2
3
|
|
Localization
Datepicker provides support for localizing its content to cater for different languages and date formats. Each localization is contained within its own file with the language code appended to the name, e.g., jquery.ui.datepicker-fr.js
for French. The desired localization file should be included after the main datepicker code. Each localization file adds its settings to the set of available localizations and automatically applies them as defaults for all instances.
The $.datepicker.regional
attribute holds an array of localizations, indexed by language code, with ""
referring to the default (English). Each entry is an object with the following attributes: closeText
, prevText
, nextText
, currentText
, monthNames
, monthNamesShort
, dayNames
, dayNamesShort
, dayNamesMin
, weekHeader
, dateFormat
, firstDay
, isRTL
, showMonthAfterYear
, and yearSuffix
.
You can restore the default localizations with:
$.datepicker.setDefaults( $.datepicker.regional[ "" ] );
And can then override an individual datepicker for a specific locale:
$( selector ).datepicker( $.datepicker.regional[ "fr" ] );
Additional Notes:
- This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.
Options
altField
""
altFormat
option to change the format of the date within this field. Leave as blank for no alternate field.altFormat
""
dateFormat
to be used for the altField
option. This allows one date format to be shown to the user for selection purposes, while a different format is actually sent behind the scenes. For a full list of the possible formats see the [[UI/Datepicker/formatDate|formatDate]] functionappendText
""
autoSize
false
true
to automatically resize the input field to accommodate dates in the current dateFormat
.beforeShow
null
beforeShowDay
null
[0]
equal to true
/false
indicating whether or not this date is selectable, [1]
equal to a CSS class name or ""
for the default presentation, and [2]
an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.calculateWeek
jQuery.datepicker.iso8601Week
changeMonth
false
changeYear
false
yearRange
option to control which years are made available for selection.closeText
"Done"
showButtonPanel
option to display this button.constrainInput
true
true
, entry in the input field is constrained to those characters allowed by the current dateFormat
option.currentText
"Today"
showButtonPanel
option to display this button.dateFormat
"mm/dd/yy"
[[UI/Datepicker/formatDate|formatDate]]
function.dayNames
[ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
dateFormat
option.dayNamesMin
[ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ]
dayNamesShort
[ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]
dateFormat
option.defaultDate
null
[[UI/Datepicker#option-dateFormat|dateFormat]]
, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.- Date: A date object containing the default date.
-
Number: A number of days from today. For example
2
represents two days from today and-1
represents yesterday. -
String: A string in the format defined by the
dateFormat
option, or a relative date. Relative dates must contain value and period pairs; valid periods are"y"
for years,"m"
for months,"w"
for weeks, and"d"
for days. For example,"+1m +7d"
represents one month and seven days from today.
duration
"normal"
gotoCurrent
false
true
, the current day link moves to the currently selected date instead of today.maxDate
null
null
, there is no maximum.- Date: A date object containing the maximum date.
-
Number: A number of days from today. For example
2
represents two days from today and-1
represents yesterday. -
String: A string in the format defined by the
dateFormat
option, or a relative date. Relative dates must contain value and period pairs; valid periods are"y"
for years,"m"
for months,"w"
for weeks, and"d"
for days. For example,"+1m +7d"
represents one month and seven days from today.
minDate
null
null
, there is no minimum.- Date: A date object containing the minimum date.
-
Number: A number of days from today. For example
2
represents two days from today and-1
represents yesterday. -
String: A string in the format defined by the
dateFormat
option, or a relative date. Relative dates must contain value and period pairs; valid periods are"y"
for years,"m"
for months,"w"
for weeks, and"d"
for days. For example,"+1m +7d"
represents one month and seven days from today.
monthNames
[ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
dateFormat
option.monthNamesShort
[ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
dateFormat
option.nextText
"Next"
numberOfMonths
1
- Number: The number of months to display in a single row.
- Array: An array defining the number of rows and columns to display.
onChangeMonthYear
null
this
refers to the associated input field.onClose
null
""
if none) and the datepicker instance as parameters. this
refers to the associated input field.onSelect
null
this
refers to the associated input field.prevText
"Prev"
selectOtherMonths
false
showOtherMonths
option is set to true
.shortYearCutoff
"+10"
[[UI/Datepicker#option-dateFormat|dateFormat]]
'y'). Any dates entered with a year value less than or equal to the cutoff year are considered to be in the current century, while those greater than it are deemed to be in the previous century.-
Number: A value between
0
and99
indicating the cutoff year. -
String: A relative number of years from the current year, e.g.,
"+3"
or"-5"
.
showAnim
"show"
"show"
(the default), "slideDown"
, "fadeIn"
, any of the jQuery UI effects. Set to an empty string to disable animation.showCurrentAtPos
0
numberOfMonths
option, the showCurrentAtPos
option defines which position to display the current month in.showMonthAfterYear
false
showOn
"focus"
"focus"
), when a button is clicked ("button"
), or when either event occurs ("both"
).showOptions
{}
showAnim
option, you can provide additional settings for that animation via this option.showOtherMonths
false
selectOtherMonths
option.showWeek
false
true
, a column is added to show the week of the year. The calculateWeek
option determines how the week of the year is calculated. You may also want to change the firstDay
option.stepMonths
1
weekHeader
"Wk"
showWeek
option to display this column.yearRange
"c-10:c+10"
"-nn:+nn"
), relative to the currently selected year ("c-nn:c+nn"
), absolute ("nnnn:nnnn"
), or combinations of these formats ("nnnn:-nn"
). Note that this option only affects what appears in the drop-down, to restrict which dates may be selected use the minDate
and/or maxDate
options.Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
1
|
|
dialog( date [, onSelect ] [, settings ] [, pos ] )Returns: jQuery (plugin only)
-
dateThe initial date.
-
onSelectType: Function()A callback function when a date is selected. The function receives the date text and date picker instance as parameters.
-
settingsType: OptionsThe new settings for the date picker.
-
posType: Number[2] or MouseEventThe position of the top/left of the dialog as
[x, y]
or aMouseEvent
that contains the coordinates. If not specified the dialog is centered on the screen.
Invoke the dialog method:
1
|
|
getDate()Returns: Date
null
if no date has been selected.- This method does not accept any arguments.
Invoke the getDate method:
1
|
|
hide()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the hide method:
1
|
|
isDisabled()Returns: Boolean
- This method does not accept any arguments.
Invoke the isDisabled method:
1
|
|
refresh()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the refresh method:
1
|
|
setDate( date )Returns: jQuery (plugin only)
Date
object or a string in the current date format (e.g., "01/26/2009"
), a number of days from today (e.g., +7
) or a string of values and periods ("y"
for years, "m"
for months, "w"
for weeks, "d"
for days, e.g., "+1m +7d"
), or null
to clear the selected date.-
dateType: DateThe new date.
Invoke the setDate method:
1
|
|
show()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the show method:
1
|
|
Example:
A simple jQuery UI Datepicker.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
|