Skip to main content
 首页 » 编程设计

js中date只能选择今天之后的时间

2022年07月15日30kerrycode

Html

    <input type="date" id="time" min="2019-12-13" />

Js

    var date = new Date(); 
    var year = date.getFullYear(); 
    var month = ('0' + (date.getMonth() + 1)).slice(-2); 
    var day = ('0' + (date.getDate())).slice(-2); 
    var day1 = ('0' + (date.getDate() + 1)).slice(-2); 
    var time = year + '-' + month + '-' + day; 
    var time1 = year + '-' + month + '-' + day1; 
    document.getElementById('time').value = time; 
    //限制不能选择今天之后的日期(加上属性max) 
    document.getElementById('time').setAttribute('min', time1);

本文参考链接:https://www.cnblogs.com/wulicute-TS/p/12034943.html