Restrict future dates in HTML5 date input

后端 未结 8 1473
误落风尘
误落风尘 2020-11-29 06:50

I want to restrict a user to only being able to add future dates in a HTML date input.

Instead of jQuery UI date picker I want to add HTML5 calender. Can anyone tell

8条回答
  •  天涯浪人
    2020-11-29 07:00

    Old Question But a solution, may help someone using JQuery:

        $(document).ready(function () {
            var today = new Date();
            var day=today.getDate()>9?today.getDate():"0"+today.getDate(); // format should be "DD" not "D" e.g 09
            var month=(today.getMonth()+1)>9?(today.getMonth()+1):"0"+(today.getMonth()+1);
            var year=today.getFullYear();
    
            $("#dpFromDate").attr('max', year + "-" + month + "-" + day);
    });
    

    The date format should be YYYY-MM-DD.

提交回复
热议问题