How do I disable dates till current date? For input Type Datetime-local

风格不统一 提交于 2021-02-05 09:42:44

问题


I am using Modal for reminder date for task. The current date and the days before it has to be disabled so that the User can select the pick-up date starting next day inorder to Mark reminder with his specified task.

for this I am using input type : <input type="datetime-local" class="form-control" autocomplete="off" id="actionreminderdate" />

If I use below java script it works for input type "Date" But not for Datetime-local.

$(function(){
    var dtToday = new Date();    
    var month = dtToday.getMonth() + 1;
    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;
     /*alert(maxDate);*/
     $('#actionreminderdate').attr('min', maxDate);
});

Can you help me with it?


回答1:


You can set min and max values on the input field:

<input type="datetime-local" id="dateInput">

$(document).ready(function(){
    elem = document.getElementById("dateInput")
    var iso = new Date().toISOString();
    var minDate = iso.substring(0,iso.length-1);
    elem.value = minDate
    elem.min = minDate
});

Get the current time in a recognised ISO format (you have to trim the zone data off the end, which is annoying), then set that as the input value and the input min.

https://jsfiddle.net/w8qgj543/24/



来源:https://stackoverflow.com/questions/50718968/how-do-i-disable-dates-till-current-date-for-input-type-datetime-local

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