The one and only datepicker with no dependencies.
Standalone:
<div id="my-datepicker"></div>
<script src="datepicker.min.js"></script>
<script>
var elem = document.querySelector('#my-datepicker');
new Datepicker(elem, {
onDateChanged: function (selectedDate) {
console.log(selectedDate);
}
});
elem.datepicker.setSelectedDate(new Date('6/22'));
</script>
With AngularJS:
<sexy-datepicker data-ng-model="selectedDate" />
<script src="angular-1.1.5.js"></script>
<script src="angular.datepicker.min.js"></script>
<script>
angular.module('myApp', ['sexyDatepicker'])
.controller('myController', function ($scope) {
$scope.selectedDate = new Date('6/22');
});
</script>
With jQuery:
<div id="my-datepicker"></div>
<script src="jquery.js"></script>
<script src="jquery.datepicker.min.js"></script>
<script>
$('#my-datepicker').datepicker({
onDateChanged: function (selectedDate) {
console.log(selectedDate);
}
});
$('#my-datepicker').get(0).datepicker.setSelectedDate(new Date('6/22'));
</script>
Going back and forth in months programatically.
/* Create datepicker with current date set to today.
July 1st. */
var datepicker = new Datepicker(...);
/* Select August 1st. */
datepicker.incrementMonth();
/* Change date to 2 months later. */
datepicker.incrementMonth(2);
/* Change it back to July 1st. */
datepicker.incrementMonth(-3);
/* Move to the next year. */
datepicker.incrementMonth(12);
/* Move back to the year selected before. */
datepicker.incrementMonth(-12);
Changing selected date.
/* New datepicker with today's date. July 1st. */
var datepicker = new Datepicker(...);
/* Make it select my birthday. */
var myBirthday = new Date('6/22/2013');
datepicker.setSelectedDate(myBirthday);
Re-build datepicker.
var datepicker = new Datepicker(...);
datepicker.build();
Remove datepicker completely.
var datepicker = new Datepicker(...);
datepicker.destroy();
Each time the date changes, the onDateChanged
option will be called with the selected date as its first argument.
/* Print the new date on the console */
new Datepicker(elem, {
onDateChanged: function (newDate) {
console.log(newDate);
}
});
It'll also call onDateChanged
when created, unless you pass a date in the options.
/* This will not trigger onDateChanged callback */
new Datepicker(elem, {
selectedDate: new Date('6/22'),
onDateChanged: function (newDate) {
console.log(newDate);
}
});
Override names of months...
/* Months in spanish */
new Datepicker(elem, {
onDateChanged: function (newDate) {
console.log(newDate);
}
monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre']
})
...and names of days too.
/* Months and days in spanish */
new Datepicker(elem, {
onDateChanged: function (newDate) {
console.log(newDate);
}
monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre'],
dayNames: ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado']
})
When using the AngularJS directive:
<!-- Add `month-names` and `day-names` attributes to element. -->
<sexy-datepicker month-names="months" day-names="days" />
angular.module('myApp', ['sexyDatepicker'])
.controller('myController', function ($scope) {
/* Create arrays with name on the $scope. */
$scope.months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre'];
$scope.days = ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'];
});
- Build with Grunt.
- Add tests.
- Something that seemed extremely important but I've forgotten.
Seriously? Thank you!
Copyright (c) 2013 Federico Baña.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.