Datepicker minDate today and maxDate 31 Dec Next year

狂风中的少年 提交于 2020-01-01 16:26:47

问题


Try to limit the date selection between today and 31st Dec of next year.

$(function() {
  $('.public-holiday-date-pick').datepicker({ 
    minDate: '0',
    yearRange: '-0:+1',
    maxDate: ???
    hideIfNoPrevNext: true
  });
});

How should I define maxDate ? tried few things like '31 12 +1', or just 'last day of next year', did not work.


回答1:


1) First get today's using

var today = new Date();

2) Similarly set the lastDate as below

var lastDate = new Date(today.getFullYear() +1, 11, 31);

The value in lastDate will be like

lastDate = 31 December, today's year +1

Finally set the lastDate as maxDate

var today = new Date();  //Get today's date
var lastDate = new Date(today.getFullYear() +1, 11, 31);  //To get the 31st Dec of next year
$(function() {
  $('.public-holiday-date-pick').datepicker({ 
    minDate: '0',
    yearRange: '-0:+1',
    maxDate: lastDate, //set the lastDate as maxDate
    hideIfNoPrevNext: true
  });
});

JSFiddle



来源:https://stackoverflow.com/questions/20086584/datepicker-mindate-today-and-maxdate-31-dec-next-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!