Top Related Projects
lightweight, powerful javascript datetimepicker with no dependencies
A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS
A datepicker for twitter bootstrap (@twbs)
JavaScript Date Range, Date and Time Picker Component
Quick Overview
Datepicker is a lightweight, customizable JavaScript date picker library. It provides an easy-to-use interface for selecting dates and date ranges, with support for various display formats and localization options.
Pros
- Lightweight and dependency-free
- Highly customizable with numerous options and methods
- Supports both single date and date range selection
- Responsive design and mobile-friendly
Cons
- Limited built-in styling options (requires custom CSS for advanced designs)
- No time picker functionality (only date selection)
- Lacks some advanced features like disabling specific dates or date ranges
Code Examples
- Basic initialization:
const elem = document.querySelector('input[name="date"]');
const datepicker = new Datepicker(elem, {
autohide: true,
format: 'mm/dd/yyyy'
});
- Date range selection:
const elem = document.querySelector('input[name="daterange"]');
const datepicker = new Datepicker(elem, {
range: true,
multipleDates: true
});
- Localization:
const elem = document.querySelector('input[name="date"]');
const datepicker = new Datepicker(elem, {
language: 'fr',
weekStart: 1 // Start week on Monday
});
Getting Started
- Install the library:
npm install @fengyuanchen/datepicker
- Include the CSS and JS files in your HTML:
<link rel="stylesheet" href="node_modules/@fengyuanchen/datepicker/dist/datepicker.min.css">
<script src="node_modules/@fengyuanchen/datepicker/dist/datepicker.min.js"></script>
- Initialize the datepicker on an input element:
const elem = document.querySelector('input[name="date"]');
const datepicker = new Datepicker(elem, {
// options here
});
Competitor Comparisons
lightweight, powerful javascript datetimepicker with no dependencies
Pros of flatpickr
- More feature-rich, including time selection and range picking
- Highly customizable with extensive theming options
- Smaller file size and better performance
Cons of flatpickr
- Steeper learning curve due to more complex API
- May be overkill for simple date picking needs
Code Comparison
datepicker:
$('.datepicker').datepicker({
format: 'yyyy-mm-dd',
autoclose: true
});
flatpickr:
flatpickr('.datepicker', {
dateFormat: 'Y-m-d',
closeOnSelect: true
});
Both libraries offer similar basic functionality, but flatpickr provides more advanced features and customization options. The code snippets show that flatpickr's syntax is slightly more concise and modern.
flatpickr is generally considered more powerful and flexible, making it suitable for complex date and time selection requirements. However, datepicker may be preferable for simpler use cases or projects where a more straightforward API is desired.
When choosing between the two, consider your project's specific needs, the level of customization required, and the trade-off between features and simplicity.
A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS
Pros of Pikaday
- Lightweight and dependency-free
- Extensive browser support, including IE8+
- Highly customizable with numerous configuration options
Cons of Pikaday
- Less actively maintained (last update in 2019)
- Fewer built-in themes and styling options
- Limited support for complex date ranges and multi-date selection
Code Comparison
Pikaday:
var picker = new Pikaday({
field: document.getElementById('datepicker'),
format: 'D MMM YYYY',
onSelect: function() {
console.log(this.getMoment().format('Do MMMM YYYY'));
}
});
Datepicker:
$('#datepicker').datepicker({
format: 'dd M yyyy',
autoHide: true,
pick: function(e) {
console.log(e.date.format('Do MMMM YYYY'));
}
});
Both libraries offer similar functionality for basic date picking, but Pikaday uses vanilla JavaScript while Datepicker relies on jQuery. Pikaday's configuration is more verbose, while Datepicker's API is more concise. Pikaday uses Moment.js for date formatting, whereas Datepicker has its own built-in formatting options.
A datepicker for twitter bootstrap (@twbs)
Pros of bootstrap-datepicker
- Extensive documentation and examples
- Built-in support for Bootstrap styling
- Wide range of customization options and events
Cons of bootstrap-datepicker
- Larger file size and potentially heavier performance impact
- Dependency on jQuery and Bootstrap (if not already used in the project)
- Less frequent updates and maintenance
Code Comparison
bootstrap-datepicker:
$('#datepicker').datepicker({
format: 'mm/dd/yyyy',
startDate: '-3d',
autoclose: true
});
datepicker:
const picker = new Datepicker(document.getElementById('datepicker'), {
format: 'mm/dd/yyyy',
autohide: true,
minDate: new Date().setDate(new Date().getDate() - 3)
});
The code comparison shows that bootstrap-datepicker uses jQuery syntax and relies on the Bootstrap framework, while datepicker uses vanilla JavaScript and has a more modern API. datepicker also provides a more flexible way to set the minimum date.
Overall, bootstrap-datepicker is better suited for projects already using Bootstrap and jQuery, offering extensive customization options. datepicker, on the other hand, is a lightweight alternative with no dependencies, making it ideal for modern web applications focused on performance and simplicity.
JavaScript Date Range, Date and Time Picker Component
Pros of Daterangepicker
- Supports date range selection, allowing users to pick start and end dates
- Offers pre-defined date ranges (e.g., Last 7 Days, This Month)
- Includes time picker functionality for more precise datetime selection
Cons of Daterangepicker
- Larger file size and more complex codebase compared to Datepicker
- Steeper learning curve due to additional features and options
- May be overkill for simple single-date selection use cases
Code Comparison
Datepicker:
$('#datepicker').datepicker({
format: 'yyyy-mm-dd',
autoclose: true
});
Daterangepicker:
$('#daterangepicker').daterangepicker({
startDate: moment().startOf('month'),
endDate: moment().endOf('month'),
ranges: {
'Today': [moment(), moment()],
'Last 7 Days': [moment().subtract(6, 'days'), moment()]
}
});
The code examples highlight the difference in complexity and functionality between the two libraries. Datepicker focuses on simple date selection, while Daterangepicker offers more advanced features like date ranges and predefined options.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Datepicker
A simple jQuery datepicker plugin.
Table of contents
- Features
- Main
- Getting started
- Options
- Methods
- Events
- I18n
- No conflict
- Browser support
- Versioning
- License
Features
- Supports options
- Supports methods
- Supports events
- Supports inline mode
- Supports touch (mobile)
- Supports internationalization
- Cross-browser support
Main
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)
Getting started
Install
npm install @chenfengyuan/datepicker
Include files:
<script src="/path/to/jquery.js"></script><!-- jQuery is required -->
<link href="/path/to/datepicker.css" rel="stylesheet">
<script src="/path/to/datepicker.js"></script>
Usage
Initialize with $.fn.datepicker
method.
<input data-toggle="datepicker">
<textarea data-toggle="datepicker"></textarea>
<div data-toggle="datepicker"></div>
$('[data-toggle="datepicker"]').datepicker();
Options
You may set datepicker options with $().datepicker(options)
.
If you want to change the global default options, You may use $.fn.datepicker.setDefaults(options)
.
autoShow
- Type:
Boolean
- Default:
false
Show the datepicker automatically when initialized.
autoHide
- Type:
Boolean
- Default:
false
Hide the datepicker automatically when picked.
autoPick
- Type:
Boolean
- Default:
false
Pick the initial date automatically when initialized.
inline
- Type:
Boolean
- Default:
false
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.
container
- Type:
Element
orString
(selector) - Default:
null
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 totrue
.
trigger
- Type:
Element
orString
(selector) - Default:
null
A element for triggering the datepicker.
language
- 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.
$().datepicker({
language: 'en-GB'
});
format
- Type:
String
- Default:
'mm/dd/yyyy'
- Available date placeholders:
- Year:
yyyy
,yy
- Month:
mm
,m
- Day:
dd
,d
- Year:
The date string format.
$().datepicker({
format: 'yyyy-mm-dd'
});
date
- Type:
Date
orString
- Default:
null
The initial date. If not set, will use the current date by default.
$().datepicker({
date: new Date(2014, 1, 14) // Or '02/14/2014'
});
startDate
- Type:
Date
orString
- Default:
null
The start view date. All the dates before this date will be disabled.
endDate
- Type:
Date
orString
- Default:
null
The end view date. All the dates after this date will be disabled.
startView
- Type:
Number
- Default:
0
- Options:
0
: days1
: months2
: years
The start view when initialized.
weekStart
- Type:
Number
- Default:
0
- Options:
0
: Sunday1
: Monday2
: Tuesday3
: Wednesday4
: Thursday5
: Friday6
: Saturday
The start day of the week.
yearFirst
- Type:
Boolean
- Default:
false
Show year before month on the datepicker header
yearSuffix
- Type:
String
- Default:
''
A string suffix to the year number.
$().datepicker({
yearSuffix: 'å¹´'
});
days
- Type:
Array
- Default:
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
Days' name of the week.
daysShort
- Type:
Array
- Default:
['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
Shorter days' name.
daysMin
- Type:
Array
- Default:
['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
Shortest days' name.
months
- Type:
Array
- Default:
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
Months' name.
monthsShort
- Type:
Array
- Default:
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Shorter months' name.
itemTag
- Type:
String
- Default:
'li'
A element tag for each item of years, months and days.
mutedClass
- Type:
String
- Default:
'muted'
A class (CSS) for muted item.
pickedClass
- Type:
String
- Default:
'picked'
A class (CSS) for picked item.
disabledClass
- Type:
String
- Default:
'disabled'
A class (CSS) for disabled item.
highlightedClass
- Type:
String
- Default:
'highlighted'
A class (CSS) for highlight date item.
template
- Type:
String
- Default:
<div class="datepicker-container">
<div class="datepicker-panel" data-view="years picker">
<ul>
<li data-view="years prev">‹</li>
<li data-view="years current"></li>
<li data-view="years next">›</li>
</ul>
<ul data-view="years"></ul>
</div>
<div class="datepicker-panel" data-view="months picker">
<ul>
<li data-view="year prev">‹</li>
<li data-view="year current"></li>
<li data-view="year next">›</li>
</ul>
<ul data-view="months"></ul>
</div>
<div class="datepicker-panel" data-view="days picker">
<ul>
<li data-view="month prev">‹</li>
<li data-view="month current"></li>
<li data-view="month next">›</li>
</ul>
<ul data-view="week"></ul>
<ul data-view="days"></ul>
</div>
</div>
The template of the datepicker.
Note: All the data-view
attributes must be set when you customize it.
offset
- Type:
Number
- Default:
10
The offset top or bottom of the datepicker from the element.
zIndex
- Type:
Number
- Default:
1
The CSS z-index
style for the datepicker.
filter
- Type:
Function
- Default:
null
- Syntax:
filter(date, view)
date
: the date for checking.view
: the the current view, one ofday
,month
oryear
.
Filter each date item. If return a false
value, the related date will be disabled.
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.
}
}
});
show
- Type:
Function
- Default:
null
A shortcut of the "show.datepicker" event.
hide
- Type:
Function
- Default:
null
A shortcut of the "hide.datepicker" event.
pick
- Type:
Function
- Default:
null
A shortcut of the "pick.datepicker" event.
Methods
Common usage:
$().datepicker('method', argument1, , argument2, ..., argumentN);
show()
Show the datepicker.
hide()
Hide the datepicker.
update()
Update the datepicker with the value or text of the current element.
pick()
Pick the current date to the element.
reset()
Reset the datepicker.
getMonthName([month[, short]])
-
month (optional):
- Type:
Number
- Default: the month of the current date
- Type:
-
short (optional):
- Type:
Boolean
- Default:
false
- Get the shorter month name
- Type:
-
(return value):
- Type:
String
- Type:
Get the month name with given argument or the current date.
$().datepicker('getMonthName'); // 'January'
$().datepicker('getMonthName', true); // 'Jan'
$().datepicker('getMonthName', 11); // 'December'
$().datepicker('getMonthName', 11, true); // 'Dec'
getDayName([day[, short[, min]])
-
day (optional):
- Type:
Number
- Default: the day of the current date
- Type:
-
short (optional):
- Type:
Boolean
- Default:
false
- Get the shorter day name
- Type:
-
min (optional):
- Type:
Boolean
- Default:
false
- Get the shortest day name
- Type:
-
(return value):
- Type:
String
- Type:
Get the day name with given argument or the current date.
$().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'
getDate([formatted])
-
formatted (optional):
- Type:
Boolean
- Default:
false
- Get a formatted date string
- Type:
-
(return value):
- Type:
Date
orString
- Type:
Get the current date.
$().datepicker('getDate'); // date object
$().datepicker('getDate', true); // '02/14/2014'
setDate(date)
- date:
- Type:
Date
orString
- Type:
Set the current date with a new date.
$().datepicker('setDate', new Date(2014, 1, 14));
$().datepicker('setDate', '02/14/2014');
setStartDate(date)
- date:
- Type:
Date
orString
ornull
- Type:
Set the start view date with a new date.
setEndDate(date)
- date:
- Type:
Date
orString
ornull
- Type:
Set the end view date with a new date.
parseDate(date)
- date:
- Type:
String
- Type:
Parse a date string with the set date format.
$().datepicker('parseDate', '02/14/2014'); // date object
formatDate(date)
- date:
- Type:
Date
- Type:
Format a date object to a string with the set date format.
$().datepicker('formatDate', new Date(2014, 1, 14)); // '02/14/2014'
destroy()
Destroy the datepicker and remove the instance from the target element.
Events
show.datepicker
This event fires when starts to show the datepicker.
hide.datepicker
This event fires when starts to hide the datepicker.
pick.datepicker
-
event.date:
- Type:
Date
- The current date
- Type:
-
event.view:
- Type:
String
- Default:
''
- Options:
'year'
,'month'
,'day'
- The current visible view
- Type:
This event fires when start to pick a year, month or day.
$().on('pick.datepicker', function (e) {
if (e.date < new Date()) {
e.preventDefault(); // Prevent to pick the date
}
});
I18n
// 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: 'å¹´'
};
<script src="/path/to/datepicker.js"></script>
<script src="/path/to/datepicker.zh-CN.js"></script>
<script>
$().datepicker({
language: 'zh-CN'
});
</script>
No conflict
If you have to use other plugin with the same namespace, just call the $.fn.datepicker.noConflict
method to revert to it.
<script src="other-plugin.js"></script>
<script src="datepicker.js"></script>
<script>
$.fn.datepicker.noConflict();
// Code that uses other plugin's "$().datepicker" can follow here.
</script>
Browser support
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Opera (latest)
- Edge (latest)
- Internet Explorer 9+
Versioning
Maintained under the Semantic Versioning guidelines.
License
MIT © Chen Fengyuan
Top Related Projects
lightweight, powerful javascript datetimepicker with no dependencies
A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS
A datepicker for twitter bootstrap (@twbs)
JavaScript Date Range, Date and Time Picker Component
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot