如何将 jquery 日期选择器限制为仅当月?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6409838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to restrict jquery date picker to only current month?
提问by sampathpremarathna
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 any in build function for it?
我想将日期选择器设置为仅显示当前月份,用户无法移动到前几个月或下个月。是否有任何构建功能?
回答by JellyBelly
Use datapiker so:
使用 datapiker 以便:
// 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
回答by Akm Sadi
$( "#datepicker" ).datepicker({
// Add this line
stepMonths: 0,
});
回答by Michel Ayres
You can use min-max
range datefrom 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).
要更改月份,只需添加要添加的defaultDate: '2m'
月份数(或添加减号'-2m'
以删除)。
Here is a Fiddlewith the working code
这是一个带有工作代码的小提琴
回答by Yanick Rochon
Use the options minDate
and maxDate
.
使用选项minDate
和maxDate
。
You can get the minDate
and maxDate
with this
你可以得到minDate
和maxDate
这个
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] });
回答by Scott C Wilson
Use changeMonth:false
and stepMonths:0
and it will work.
使用changeMonth:false
和stepMonths:0
它会起作用。
$('#calendar').datepicker({
changeMonth: false,
stepMonths: 0,
dateFormat: "mm/dd/yy",
firstDay: 1,
}).datepicker("setDate", "+0d" );
Working fiddle: https://jsfiddle.net/scottcwilson/phkkt20e/2/
回答by Kule
// 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
});
回答by Tomgrohl
If your using jQuery UI Datepicker, this would work:
如果您使用 jQuery UI Datepicker,这将起作用:
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
});