Disable certain dates from html5 datepicker

2019-01-02 18:26发布

Is it possible to disable dates when I use I want to disable current date for one scenario and future dates for other scenario. How should I disable the dates?

3条回答
不再属于我。
2楼-- · 2019-01-02 19:04

You could use this to disable future dates :
Inside you document.ready function, place

//Display Only Date till today //

var dtToday = new Date();
var month = dtToday.getMonth() + 1;     // getMonth() is zero-based
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if(month < 10)
   month = '0' + month.toString();
if(day < 10)
   day = '0' + day.toString();

var maxDate = year + '-' + month + '-' + day;
$('#dateID').attr('max', maxDate);

and in form

<input id="dateID" type="date"/>

Here is the working jFiddle Demo

查看更多
看风景的人
3楼-- · 2019-01-02 19:05

HTML datepicker (<input type=date>) supports min/max attribute, but it is not widely supported.

At the meantime you may consider using bootstrap-datepicker, v1.2.0 is on github.

References:

W3C spec

查看更多
初与友歌
4楼-- · 2019-01-02 19:08

You can add a min or max attribute to the input type=date. The date must be in ISO format (yyyy-mm-dd). This is supported in many mobile browsers and current versions of Chrome, although users can manually enter an invalid date without using the datepicker.

<input name="somedate" type="date" min="2013-12-25">

The min and max attributes must be a full date; there's no way to specify "today" or "+0". To do that, you'll need to use JavaScript or a server-side language:

var today = new Date().toISOString().split('T')[0];
document.getElementsByName("somedate")[0].setAttribute('min', today);

http://jsfiddle.net/mblase75/kz7d2/

Ruling out only today, while allowing past or future dates, is not an option with here. However, if you meant you want tomorrow to be the min date (blanking out today and all past dates), see this question to increment today by one day.

As in all other cases involving HTML forms, you should always validate the field server-side regardless of how you constrain it client-side.

查看更多
登录 后发表回答