Datepicker minDate today and maxDate 31 Dec Next y

2019-04-13 12:06发布

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条回答
爷的心禁止访问
2楼-- · 2019-04-13 12:54

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

查看更多
登录 后发表回答