简体   繁体   中英

How to increment date and how to disable previous dates in javascript?

Im using html5 datepicker like [input type="date"],

I just need to increment current date + 1. i mean show the tomorrow date and future dates only, the current date and previous dates wants to go disable state only show the future dates..

anyone please help me..

here is my tried code.

 // Disable date var input = document.getElementById("preDate"); var today = new Date(); var day = today.getDate()+1; var mon = new String(today.getMonth()+1); //January is 0! var yr = today.getFullYear(); if (mon.length < 0) { mon = "0" + mon; } var date = new String(yr + '-' + mon + '-' + day); input.disabled = false; input.setAttribute('min', date);
 <div class="form-group col-3 rfdate"> <label class="col-sm-6 control-label p-sm-0">Refund Date</label> <input type="date" class="form-control" id="preDate" name="preDate" /> </div>

Fiddle: Code Here..

Try this:

 // Disable date var input = document.getElementById("preDate"); var today = new Date(); var day = today.getDate()+1; var mon = new String(today.getMonth()+1); //January is 0! var yr = today.getFullYear(); if (mon.length > 0) { mon = `0${mon}`; } if (day < 10) { day = `0${day}`; } var date = `${yr}-${mon}-${day}`; input.disabled = false; input.setAttribute('min', date); input.value = date; console.log(date);
 <div class="form-group col-3 rfdate"> <label class="col-sm-6 control-label p-sm-0">Refund Date</label> <input type="date" class="form-control" id="preDate" name="preDate" /> </div>

Tomorrows date is smart created like that:

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM