Set date input field's max date to today

前端 未结 10 629
说谎
说谎 2020-12-02 19:37

I just have a simple line of code like this:


Is there a sim

10条回答
  •  春和景丽
    2020-12-02 20:34

    You will need Javascript to do this:

    HTML

    
    

    JS

    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
     if(dd<10){
            dd='0'+dd
        } 
        if(mm<10){
            mm='0'+mm
        } 
    
    today = yyyy+'-'+mm+'-'+dd;
    document.getElementById("datefield").setAttribute("max", today);
    

    JSFiddle demo

提交回复
热议问题