Dateinput
This tool brings HTML5 date input to all browsers in such a way that you can make it look and behave like you want. This small screen candy weights only 2.0 Kb.
When the calendar is visible you can navigate between days with your Arrow Keys or with VIM keyboard shortcuts HJKL. PageUp and PageDown navigates between months and Enter selects the day. The Home key will navigate to the present day and backspace clears the input.
HTML5 date input
This tool brings HTML5 <input type="date" /> support to all browsers in a consistent and standardized manner today. The date field is just like any other form field which can be scripted and submitted. Opera will render date input even if JavaScript is disabled.
For nerds and humans
Call $(":date").dateinput(); and all your HTML5 date inputs are working. End users can select dates with the mouse or with the keyboard arrow keys. Hackers can use VIM keyboard shortcuts (HJKL) to navigate between dates and months.
Styleable
You can design calendars that fit the overall feel of your site. You can use images and the latest CSS3 techniques if you like. CSS class names can be changed and tables are not used, so you can even design your own layout: be it single row or vertical. Professional example skins are provided, and you can use them for free.
File size: 2.0 Kb
This tool give you all the features and configuration options you'll possibly need, such as localization, formatting, year/selectors, a scripting API and an event model. A smaller codebase is easier to control and results in snappier behaviour.
Demos
- Minimal setup for dateinput
- A large skin for Dateinput
- Customizing Dateinput behavior
- Prompting for start and end dates
- Calendar that is always available
- Localizing the Dateinput (french)
Configuration
Property | Default | Description |
---|---|---|
css | Object |
Values for the id and class attributes of the dateinput. See skinning for more details. |
firstDay | 0 |
Specifies the first day of the week. Sunday = 0, Monday = 1 and so on …. |
format | 'mm/dd/yy' |
Specifies how the date value is formatted in the input field. See formatting for more details. |
lang | 'en' |
Language to be used. Only English is built-in, but localization is possible. |
max | 0 |
Latest selectable date. See supplying date values for more details. |
min | 0 |
Earliest selectable date. See supplying date values for more details. |
offset | [0, 0] |
The dateinput is positioned right below the input field. You can fine tune the position with this property. For example, the value [10, -20] moves the tooltip position 10px downwards and 20px to the left. The first value modifies the vertical positioning from the top edge of the tooltip and the second value modifies the horizontal positioning from the left edge of the tooltip. Positive values move the tooltip downwards (first value) or to the right (second value), while negative values move the tooltip upwards (first value) or to the left (second value). |
selectors | false |
Whether month and year dropdown menus are displayed. |
speed | 100 |
The speed (in milliseconds) at which the dateinput reveals itself. |
trigger | false |
Whether a trigger element is generated after the input field. This is an A tag with a CSS class name caltrigger (can be changed) that opens up the calendar when clicked. |
value | Initial value to be displayed for the user. This can either be a valid date value or any string such as "Today". | |
yearRange | [-5, 5] |
Range of years displayed relative to the current year. |
Input attributes
You can also configure the dateinput by supplying attributes for the input field. For example:
Here are all attributes supported by this tool. They are all are valid HTML5 attributes.
Attribute | Description |
---|---|
disabled | Whether the dateinput is completely disabled. The user cannot focus on the input element at all. This attribute can be toggled dynamically by scripting. |
max | Latest selectable date. See supplying date values for more information. |
min | Earliest selectable date. See supplying date values for more information. |
readonly | When enabled, the dateinput can only be opened and browsed but no date can be selected. This attribute can be toggled dynamically by scripting. |
value | Initial value. See supplying date values for more information. |
data-value | Initial date value. This is the primary means of providing the initial value because the value attribute is shown to the user and is typically formatted or it can be any string such as "Today". |
Supplying date values
This tool accepts date values from various places: configuration, JavaScript API and from input attributes.
You can supply the value in three different ways.
- A rfc3339 formatted string such as 2015-11-25
- A number such as 20. this is the number of days from the present date. A positive value moves forward (to the future) and a negative value goes backward (to the past)
- A JavaScript Date object
Formatting
Formatting specifies how the dates are displayed for the user. The formatting pattern can be set in the format configuration variable or fed as argument to the getDate() function in the API. Here are a few examples:
Formatting characters can be mixed with other characters and you can specify as many as you like.
List of formatting characters
Characters | Description |
---|---|
d | Day of the month as digits; no leading zero for single-digit days. |
dd | Day of the month as digits; leading zero for single-digit days. |
ddd | Day of the week as a three-letter abbreviation. |
dddd | Day of the week represented by its full name. |
m | Month as digits; no leading zero for single-digit months. |
mm | Month as digits; leading zero for single-digit months. |
mmm | Month as a three-letter abbreviation. |
mmmm | Month represented by its full name. |
yy | Year as last two digits; leading zero for years less than 10. |
yyyy | Year represented by four digits. |
Skinning
Skinning is the same as writing CSS against the dateinput HTML layout. Here is an example CSS file and the default HTML layout. This tool allows you to customize the id and class names with the css configuration option. For example:
Prefix
All ids and class names are prefixed by the value specified in the prefix property, allowing you to give them one common "namespace". This avoids potential clashes with other CSS names in your web application. By setting this value to null the prefix is not used, allowing you to use shorter names. The following table lists all properties supported by the css configuration option. By default the tool uses a prefix "cal" which is colored blue in the default value column.
CSS names
Name | Type | Element | Default value | Description |
---|---|---|---|---|
input | class | input | date | The date input field |
root | id | div | calroot | The root element for the dateinput. |
head | id | div | calhead | Dateinput header that contains next/prev month buttons and dropdown menus. |
title | id | div | caltitle | Title of the dateinput. |
prev | id | a | calprev | Previous month link. |
next | id | a | calnext | Next month link. |
disabled | class | a | caldisabled | Class name for a prev/next month navigation button which cannot be pressed. |
month | id | select | calmonth | Month selector. |
year | id | select | calyear | Year selector. |
body | id | div | calbody | Dateinput body that contains the day names and the weeks. |
days | id | div | caldays | Container for day names. Individual days are span elements. |
weeks | id | div | calweeks | Container for weeks. |
week | class | div | calweek | Container for days. Individual days are a elements. |
off | class | a | caloff | Class name for a day that is not within range (older or newer than max/min days). |
sunday | class | a | calsunday | Class name for Sundays. |
current | id | a | calcurrent | id for the currently selected day. |
today | id | a | caltoday | id for the present day. |
focus | class | a | calfocus | Class name for a day that has been focused via keyboard navigation. |
trigger | class | a | caltrigger | Class name for the trigger element after the input field if the trigger configuration variable is enabled |
Scripting API
First make sure you have familiarized yourself with jQuery Tools scripting.
Method | Return value | Description |
---|---|---|
addDay(amount) | API |
Adds days to the currently selected date. The amount of days is specified in the argument. Without an argument one day is added and a negative argument moves backwards. Does not have any effect when the calendar is opened. |
addMonth(amount) | API |
Adds months to the currently selected date. The amount of months is specified in the argument. Without an argument one month is added and a negative argument moves backwards. If you call this method while the calendar is opened then the calendar seeks forward and backward but the value does not change. |
addYear(amount) | API |
Adds years to the currently selected date. The amount of years is specified in the argument. Without an argument one year is added and a negative argument moves backwards. If you call this method while the calendar is opened then the calendar seeks forward and backward but the value does not change. |
getConf() | Object |
Returns the configuration of the current instance. |
getCalendar() | jQuery |
Returns the root element for the calendar. |
getInput() | jQuery |
Returns the input field associated with the dateinput. |
getValue() | Date |
Returns the currently selected date. |
getValue(format) | String |
Returns the current date, formatted with the pattern given as the argument. |
hide() | API |
Hides the calendar. |
isOpen() | boolean |
Whether calendar is opened (true) or closed (false). |
setMax(value, [fit]) | API |
Sets the latest selectable date. See supplying date values. If the fit argument is true and the current value is later than the maximum value then the current value is changed to the maximum value. By default fit is false. |
setMin(value, [fit]) | API |
Earliest selectable date. See supplying date values. If the fit argument is true and the current value is earlier than the minimum value then the current value is changed to the minimum value. By default fit is false. |
setValue(date) | API |
Sets the current date to the one given in the argument. Must be a JavaScript Date object. If the argument is empty and the calendar is opened then the calendar is moved to the month where the currently selected date is. |
setValue(year, month, day) | API |
Sets the current date. |
show() | API |
Shows the calendar. |
today() | API |
Goes to today. |
Events
First make sure you have read about Events in jQuery Tools. All event listeners receive the Event Object as the first argument. Here is an example:
Event | When does it occur? |
---|---|
onBeforeShow | Before the dateinput is shown. |
onShow | After the dateinput is completely shown (after an optional animation). |
change | When a date was selected and the field value has been changed. Note that the name is the same as the jQuery's change method and that you can bind the event directly with this function. For other events you must use the bind method. This leads to more fluent syntax as demonstrated in the example above. |
onHide | When the dateinput is closed. Returning false here will not hide the calendar. |
Localization
You can localize the dateinput to your language with a static $.tools.dateinput.localize call, after which you can use the dateinput with another language. The tool uses the following call internally to enable English localization:
To add the French localization you should do the following:
Note: the '+' above is only to indicate the list of months is shown on two lines.
Notice that all special characters need to be escaped. I used this list to find the special entities for French characters. After the localization is in place you can use the French language by supplying lang: 'fr' in your configuration. Or you can make French the default language by editing the global configuration as follows: $.tools.dateinput.conf.lang = 'fr';