如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:48:33  来源:igfitidea点击:

how to restrict jquery date picker to only current month?

jqueryjquery-ui

提问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

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

回答by Akm Sadi

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

    stepMonths: 0,
});

回答by Michel Ayres

You can use min-maxrange datefrom 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 Fiddlewith the working code

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

回答by Yanick Rochon

Use the options minDateand maxDate.

使用选项minDatemaxDate

You can get the minDateand maxDatewith this

你可以得到minDatemaxDate这个

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:falseand stepMonths:0and it will work.

使用changeMonth:falsestepMonths: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/

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