Download
User Rating: 4.6/5 ( 1 votes)
I think most jQuery developers at least know about jQuery UI. It is a library for building common interface features such as progress bars, tabs, tooltips, and accordion menus. Luckily there is also a component for rendering input date pickers using jQuery UI Datepicker. The codes are very simple and follow the same formatting you would expect with any other jQuery plugin.
The library comes equipped with more dynamic features than the basic examples above. You can now include date ranges where users select 2 different dates for 2 different input fields. Then using these values it is possible to combine a full date period for querying articles and similar data requests. It will take a bit of time customizing the styles, but it is definitely an exciting option for JS developers. Just remember this plugin will require dependencies for jQuery and jQuery UI libraries, along with the jQuery UI theme stylesheet
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.
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.
Code examples:
Set all date pickers to open on focus or a click on an icon.
$.datepicker.setDefaults({
showOn: "both",
buttonImageOnly: true,
buttonImage: "calendar.gif",
buttonText: "Calendar"
});
Set all date pickers to have French text.
$.datepicker.setDefaults( $.datepicker.regional[ "fr" ] );
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)
Code examples:
Display the date in ISO format. Produces "2007-01-26".
$.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) );
Display the date in expanded 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 )
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'
Code examples:
Extract a date in ISO format.
$.datepicker.parseDate( "yy-mm-dd", "2007-01-26" );
Extract a date in expanded 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 )
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.
Code examples:
Find the week of the year for a date.
$.datepicker.iso8601Week( new Date( 2007, 1 - 1, 26 ) );
$.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.
Code examples:
Set the DatePicker so no weekend is selectable
$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends
});