如何将jquery日期选择器限制为仅当前月份?

时间:2022-09-27 20:13:54

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?

我想设置日期选择器只显示当前月份,用户无法移动到前几个月或下个月。是否有任何构建功能?

7 个解决方案

#1


6  

$( "#datepicker" ).datepicker({ 
    // Add this line

    stepMonths: 0,
});

#2


6  

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

文档:http://jqueryui.com/demos/datepicker/#min-max

#3


2  

You can use min-max range date from datepicker

您可以使用datepicker中的min-max范围日期

$('#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 Fiddle with the working code

这是一个工作代码的小提琴

#4


1  

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] });

#5


1  

// 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
});

#6


0  

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 
});

#7


0  

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/

工作小提琴:https://jsfiddle.net/scottcwilson/phkkt20e/2/

#1


6  

$( "#datepicker" ).datepicker({ 
    // Add this line

    stepMonths: 0,
});

#2


6  

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

文档:http://jqueryui.com/demos/datepicker/#min-max

#3


2  

You can use min-max range date from datepicker

您可以使用datepicker中的min-max范围日期

$('#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 Fiddle with the working code

这是一个工作代码的小提琴

#4


1  

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] });

#5


1  

// 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
});

#6


0  

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 
});

#7


0  

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/

工作小提琴:https://jsfiddle.net/scottcwilson/phkkt20e/2/