- lightweight, customizable, no dependencies -
This is the minimum markup and it renders the Datepicker as a static element on the page. If you want it dynamically hidden or shown on an event you have to do this by yourself.
I'm sure you can do it 😉.
<input id="input" type="text" name="date">
<div id="container">
<!-- the Datepicker will be rendered here -->
</div>
// best wait for DOM ready
document.addEventListener("DOMContentLoaded", function () {
// constructor
var datepicker = new Datepicker({
container: document.querySelector("#container"), // container
onSelect: onSelect // callback
});
// get a Date object as parameter to work with
function onSelect (date) {
document.querySelector("#input").value = date.toDateString();
}
}
- date [Date, null] - The current Date of the Datepicker, if set.
- setDate (date) - Sets the value of the Datepicker, it must be a Date object or parsable through 'new Date()', returns the new date as Date object.
- clearTime () - Clears the datepickers value.
- destroy - destroys the datepicker. Returns null.
- container - The container where the Datepicker will be rendered into.
- onSelect (date) - Callback that returns the selected Date.
- firstDay (Default: 0) - Starting day of the week from 0-6 (0 = Sunday, 6 = Saturday).
- minDate (Default: null) - Defines the minimum available Date.
- maxDate (Default: null) - Defines the maximum available Date.
- startMode (Default: day) - Current mode of the Datepicker (day|month|year)*. Defines the mode initialized.
- customClass ({date: date, mode: mode}) - Optional function to add classes to dates based on passing an object with date and current mode properties.
- disableDate ({date: date, mode: mode}) - Optional function to disabled Dates based on passing an object with date and current mode properties.
- The Datepicker has i18n support through dedicated script files.
- Simply add a script from the i18n directory before the datepicker script and it automatically uses this language.
- npm install - Installs dependencies.
- npm run uglify - Minifies the datepicker.js file for production use.
There are many other Datepicker plugins out there. But I wanted to build one on my own and to share my work.
This is not meant to be an all in one solution. This is from a developer for developers.
Feel free to try, fork and share. I appreciate every feedback.