Imagine you have this in HTML:
<input type="text" id="date_start">
In javascript we have this:
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var fSDate = new Date(y, m, 1);
var fEDate = new Date(y - 1, m + 1, 0);
$.datepicker.setDefaults({
changeMonth: false,
changeYear: false,
showOtherMonths: false,
yearRange: '-0:+0',
dateFormat: 'yy-mm-dd',
defaultDate: +0, //30 days ago
numberOfMonths: 1,
minDate: fSDate,
maxDate: fEDate,
showAnim: 'fadeIn',
showButtonPanel: false
});
$('#date_start').datepicker();
Pay attention to fSDate
and fEDate
and notice how you can set limits for year, month, and day using a little logic. I already subtracted a year in fEDate
and it will only show at most 2016.
See this online sample in JSFiddle. This example is great for you to see everything that can change until you get the desired result. Play this fiddler a little bit until you master this datepicker()
.