Specify only YEAR and MONTH for input date min/max attributes

11,777

Solution 1

You could use something like this:

<input class="month-date" type="date" max="2014-02">
var els = document.getElementsByClassName("month-date");
for(var i=0, l=els.length; i<l; ++i) {
    var max = els[i].getAttribute('max').split('-');
    max.push(new Date(max[0], max[1], 0).getDate());
    els[i].max = max.join('-');
}

Demo

Solution 2

No, you cannot do that with HTML. The max value in in <input type="date" ...> must by definition be a specific date, and it must be a valid date.

You should calculate the last day of the intended month when you generate the HTML markup.

Share:
11,777
kratos
Author by

kratos

Updated on June 26, 2022

Comments

  • kratos
    kratos almost 2 years

    i have the following:

    <input type="date" max="2014-10-31">
    

    Can i specify only the year and the month? I guess it'd be like:

    <input type="date" max="2014-10-xx">
    

    or just

    <input type="date" max="2014-10">
    

    (assuming the format is yyyy/mm/dd).

    And HTML does the rest. I can't specific the day, only year and month (given by server-side), because there are different max days for each month (like february, which has 28). So, can not be 31 the max.

    when i put 31 [day] for max attribute, the months that have 28, 30 stop working, i mean, the input gets weird and so i can modify the month too, but it can't happen cause the user should be able only to change the day - the month and the days are specific.