I want to set the date-picker to show only the current month and user cannot move to previous months or next months.Is there a开发者_Python百科ny in build function for it?
Use datapiker so:
// temp vars used below
var currentTime = new Date()
var minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), +1); //one day next before month
var maxDate = new Date(currentTime.getFullYear(), currentTime.getMonth() +2, +0); // one day before next month
$( "#datepicker" ).datepicker({
minDate: minDate,
maxDate: maxDate
});
Documentation: http://jqueryui.com/demos/datepicker/#min-max
$( "#datepicker" ).datepicker({
// Add this line
stepMonths: 0,
});
You can use min-max
range date from datepicker
$('#datepicker').datepicker({ minDate: -20, maxDate: "+1M +10D" });
Or just some configurations
$('#datepicker').datepicker( {
changeMonth: false,
changeYear: false,
stepMonths: false,
dateFormat: 'dd MM'
});
To change the month just add the defaultDate: '2m'
with the number of month that you want to add (or add a minus like '-2m'
to remove).
Here is a Fiddle with the working code
Use the options minDate
and maxDate
.
You can get the minDate
and maxDate
with this
function getMinMaxCurrentDate() {
var d = new Date();
var day = d.getDate(); // range 1-31
var month = d.getMonth() + 1; // range 1-12
var year = d.getFullYear(); // ie. (2011)
var max;
if (month <= 7) {
if (month == 2) {
// check for leap years for Febuary
var isLeap = new Date(year,1,29).getDate() == 29;
max = 28 + (isLeap ? 1 : 0);
} else {
max = (month & 1) ? 31 : 30;
}
} else {
max = (month & 1) ? 30 : 31;
}
return [-day, max - day];
}
var minMax = getMinMaxCurrentDate();
$( "#datepicker" ).datepicker({ minDate: minMax[0], maxDate: minMax[1] });
Use changeMonth:false
and stepMonths:0
and it will work.
$('#calendar').datepicker({
changeMonth: false,
stepMonths: 0,
dateFormat: "mm/dd/yy",
firstDay: 1,
}).datepicker("setDate", "+0d" );
Working fiddle: https://jsfiddle.net/scottcwilson/phkkt20e/2/
// temp vars used below
var date = new Date();
// get the current date
var minDate = new Date(date.getFullYear(), date.getMonth()+ testMounth, 1);
var maxDate = new Date(date.getFullYear(), date.getMonth() +1 +testMounth, -0);
$('#datepicker').datepicker({
hideIfNoPrevNext: true,
minDate: minDate,
maxDate: maxDate
});
If your using jQuery UI Datepicker, this would work:
function getDaysInMonth(y,m){
return (new Date(y,m,0)).getDate() + 1;
}
var td= new Date();
var minDate = new Date( td.getYear(), td.getMonth(), +1, +getDaysInMonth( td.getYear(), td.getMonth() ) );
var maxDate = new Date( td.getYear(), td.getMonth(), +1, 0);
$( "#datepicker" ).datepicker({
minDate: minDate,
maxDate: maxDate
});
精彩评论