A simple jQuery datepicker plugin.
npm install @chenfengyuan/datepicker  
> A simple jQuery datepicker plugin.
- Website
- Features
- Main
- Getting started
- Options
- Methods
- Events
- I18n
- No conflict
- Browser support
- Versioning
- License
- Supports options
- Supports methods
- Supports events
- Supports inline mode
- Supports touch (mobile)
- Supports internationalization
- Cross-browser support
``text`
dist/
├── datepicker.css
├── datepicker.min.css (compressed)
├── datepicker.js (UMD)
├── datepicker.min.js (UMD, compressed)
├── datepicker.common.js (CommonJS, default)
└── datepicker.esm.js (ES Module)
`shell`
npm install @chenfengyuan/datepicker
Include files:
`html`
Initialize with $.fn.datepicker method.
`html`
`js`
$('[data-toggle="datepicker"]').datepicker();
You may set datepicker options with $().datepicker(options).$.fn.datepicker.setDefaults(options)
If you want to change the global default options, You may use .
- Type: Booleanfalse
- Default:
Show the datepicker automatically when initialized.
- Type: Booleanfalse
- Default:
Hide the datepicker automatically when picked.
- Type: Booleanfalse
- Default:
Pick the initial date automatically when initialized.
- Type: Booleanfalse
- Default:
Enable inline mode.
If the element is not an input element, will append the datepicker to the element.
If the container option is set, will append the datepicker to the container.
- Type: Element or String(selector)null
- Default:
A element for putting the datepicker. If not set, the datepicker will be appended to document.body by default.
> Only works when the inline option set to true.
- Type: Element or String(selector)null
- Default:
A element for triggering the datepicker.
- Type: String''
- Default:
The ISO language code. If not set, will use the built-in language (en-US) by default.
> You should define the language first. View the I18n section for more information or check out the i18n folder for available languages.
`js`
$().datepicker({
language: 'en-GB'
});
- Type: String'mm/dd/yyyy'
- Default: yyyy
- Available date placeholders:
- Year: , yymm
- Month: , mdd
- Day: , d
The date string format.
`js`
$().datepicker({
format: 'yyyy-mm-dd'
});
- Type: Date or Stringnull
- Default:
The initial date. If not set, will use the current date by default.
`js`
$().datepicker({
date: new Date(2014, 1, 14) // Or '02/14/2014'
});
- Type: Date or Stringnull
- Default:
The start view date. All the dates before this date will be disabled.
- Type: Date or Stringnull
- Default:
The end view date. All the dates after this date will be disabled.
- Type: Number0
- Default: 0
- Options:
- : days1
- : months2
- : years
The start view when initialized.
- Type: Number0
- Default: 0
- Options:
- : Sunday1
- : Monday2
- : Tuesday3
- : Wednesday4
- : Thursday5
- : Friday6
- : Saturday
The start day of the week.
- Type: Booleanfalse
- Default:
Show year before month on the datepicker header
- Type: String''
- Default:
A string suffix to the year number.
`js`
$().datepicker({
yearSuffix: '年'
});
- Type: Array['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
- Default:
Days' name of the week.
- Type: Array['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
- Default:
Shorter days' name.
- Type: Array['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
- Default:
Shortest days' name.
- Type: Array['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
- Default:
Months' name.
- Type: Array['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
- Default:
Shorter months' name.
- Type: String'li'
- Default:
A element tag for each item of years, months and days.
- Type: String'muted'
- Default:
A class (CSS) for muted item.
- Type: String'picked'
- Default:
A class (CSS) for picked item.
- Type: String'disabled'
- Default:
A class (CSS) for disabled item.
- Type: String'highlighted'
- Default:
A class (CSS) for highlight date item.
- Type: String
- Default:
`html`
The template of the datepicker.
Note: All the data-view attributes must be set when you customize it.
- Type: Number10
- Default:
The offset top or bottom of the datepicker from the element.
- Type: Number1
- Default:
The CSS z-index style for the datepicker.
- Type: Functionnull
- Default: filter(date, view)
- Syntax: date
- : the date for checking.view
- : the the current view, one of day, month or year.
Filter each date item. If return a false value, the related date will be disabled.
`js
var now = Date.now();
$().datepicker({
filter: function(date, view) {
if (date.getDay() === 0 && view === 'day') {
return false; // Disable all Sundays, but still leave months/years, whose first day is a Sunday, enabled.
}
}
});
`
- Type: Functionnull
- Default:
A shortcut of the "show.datepicker" event.
- Type: Functionnull
- Default:
A shortcut of the "hide.datepicker" event.
- Type: Functionnull
- Default:
A shortcut of the "pick.datepicker" event.
Common usage:
`js`
$().datepicker('method', argument1, , argument2, ..., argumentN);
Show the datepicker.
Hide the datepicker.
Update the datepicker with the value or text of the current element.
Pick the current date to the element.
Reset the datepicker.
- month (optional):
- Type: Number
- Default: the month of the current date
- short (optional):
- Type: Booleanfalse
- Default:
- Get the shorter month name
- (return value):
- Type: String
Get the month name with given argument or the current date.
`js`
$().datepicker('getMonthName'); // 'January'
$().datepicker('getMonthName', true); // 'Jan'
$().datepicker('getMonthName', 11); // 'December'
$().datepicker('getMonthName', 11, true); // 'Dec'
- day (optional):
- Type: Number
- Default: the day of the current date
- short (optional):
- Type: Booleanfalse
- Default:
- Get the shorter day name
- min (optional):
- Type: Booleanfalse
- Default:
- Get the shortest day name
- (return value):
- Type: String
Get the day name with given argument or the current date.
`js`
$().datepicker('getDayName'); // 'Sunday'
$().datepicker('getDayName', true); // 'Sun'
$().datepicker('getDayName', true, true); // 'Su'
$().datepicker('getDayName', 6); // 'Saturday'
$().datepicker('getDayName', 6, true); // 'Sat'
$().datepicker('getDayName', 6, true, true); // 'Sa'
- formatted (optional):
- Type: Booleanfalse
- Default:
- Get a formatted date string
- (return value):
- Type: Date or String
Get the current date.
`js`
$().datepicker('getDate'); // date object
$().datepicker('getDate', true); // '02/14/2014'
- date:
- Type: Date or String
Set the current date with a new date.
`js`
$().datepicker('setDate', new Date(2014, 1, 14));
$().datepicker('setDate', '02/14/2014');
- date:
- Type: Date or String or null
Set the start view date with a new date.
- date:
- Type: Date or String or null
Set the end view date with a new date.
- date:
- Type: String
Parse a date string with the set date format.
`js`
$().datepicker('parseDate', '02/14/2014'); // date object
- date:
- Type: Date
Format a date object to a string with the set date format.
`js`
$().datepicker('formatDate', new Date(2014, 1, 14)); // '02/14/2014'
Destroy the datepicker and remove the instance from the target element.
This event fires when starts to show the datepicker.
This event fires when starts to hide the datepicker.
- event.date:
- Type: Date
- The current date
- event.view:
- Type: String''
- Default: 'year'
- Options: , 'month', 'day'
- The current visible view
This event fires when start to pick a year, month or day.
`js`
$().on('pick.datepicker', function (e) {
if (e.date < new Date()) {
e.preventDefault(); // Prevent to pick the date
}
});
`js`
// datepicker.zh-CN.js
$.fn.datepicker.languages['zh-CN'] = {
format: 'yyyy年mm月dd日',
days: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
daysShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
daysMin: ['日', '一', '二', '三', '四', '五', '六'],
months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
monthsShort: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
weekStart: 1,
startView: 0,
yearFirst: true,
yearSuffix: '年'
};
`html`
If you have to use other plugin with the same namespace, just call the $.fn.datepicker.noConflict method to revert to it.
`html``
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Opera (latest)
- Edge (latest)
- Internet Explorer 9+
Maintained under the Semantic Versioning guidelines.