There are many great datepickers available on GitHub, however, I didn't find one that really suited my needs. The hardest thing to find is a datepicker that can do a specific combination of features.
Here's a few other reasons:
<input type="date"/>
doesn't offer Of course, no one datepicker will serve all projects since your needs may be different. So I'll be adding more to this API in the near future. You may also suggest changes by forking this repo and creating a pull request or opening an issue.
I am the creator, and currently contribute and maintain this project almost entirely myself. I have reviewed and accepted some pull requests along the way however, and I am happy to review and merge any helpful PRs as soon as I find time. If you want to contribute checkout the Contributing section!
Want to test the Datepicker live? Checkout the full documentation, and a sandbox to test different options:
Since the goal of this project is to use only vanilla JavaScript and Moment.js, we have only one dependency:
git clone https://github.com/ctrachte/Datepicker.js
Datepicker.js
to implement with pure Vanilla JS, HTML and CSS.Datepicker.html
in your browser of choice to view and test behavior.npm i --save moment-datepicker-js
Import the Datepicker, and move/scope the DatepickerSmall.css and moment.js files in the appropriate places in your project. Below is a basic example component that should work in Next.js or React for simple implementations:
import momentDatepicker from 'moment-datepicker-js'
//With newer versions of React you can use and manipulate the DOM via hooks like this:
import React, { useEffect, useRef } from 'react'
const Datepicker = () => {
const myContainer = useRef(null)
let datepicker
useEffect(() => {
datepicker = new momentDatepicker({ containerElement: myContainer.current })
// console.log(myContainer.current, datepicker)
})
return (
<>
// note we are using the Content Delivery Network reference for Moment.js, you can also install moment seperately, or use a basic version from the /helpers directory
<script
src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"
integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ=="
crossOrigin="anonymous"
referrerPolicy="no-referrer"
></script>
<h1>Datepicker</h1>
<div ref={myContainer}></div>
</>
)
}
export default Datepicker
1.) You will first need a recent version of Moment.js installed.
2.) Download and add the Datepicker.js and Datepicker.css files to their appropriate directories in your project.
3.) Below are all the options you can enable using Vanilla JS syntax. For implementation in React, see above NPM section
// there should only be this one variable here
// to instantiate the class into the container element
const testDatepicker = new Datepicker({
containerElement: document.querySelector('.DatepickerContainer'),
presetMenu: true,
singleDate: false,
autoClose: false,
timePicker: false,
leadingTrailingDates: true,
clearDates: true,
defaults: false,
// militaryTime: false,
// max: moment("08/14/2022"),
// min: new Date("08/14/2021"),
// defaults: [new Date('08/14/2023'),new Date('08/14/2023')],
menuOptions:
[
{ title: 'Today',
values: [new Date(), new Date()]
}
],
startDateLabel: "Reservation Start: ",
endDateLabel: "Reservation End: ",
// onChange: function () {
// console.log("onChange:", this.dates);
// },
// onSubmit: function () {
// console.log("onSubmit:", this.dates);
// },
// onClose: function () {
// console.log("onClose:", this.dates);
// }
});
4.) Adjust the options above to meet the needs of your project, or the project's component you are implementing the datepicker in.
Contributions are greatly appreciated towards the final goal of a perfect datepicker!
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Code will be reviewed before being merged. If your code does not quite work or needs revision it may not be merged to the master.
Distributed under the MIT License.
Caleb Trachte - contact info available for close friends and associates only.
Project Link: https://github.com/ctrachte/Datepicker.js/projects/1
Want to fund the development of this project? Donate crypto!
Latest ✔ | Latest ✔ | Not supported ❌ | Not tested | Latest ✔ | Latest ✔ |