var todaysDate = new Date(); // Gets today's date
// Max date attribute is in "YYYY-MM-DD". Need to format today's date accordingly
var year = todaysDate.getFullYear(); // YYYY
var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2); // MM
var day = ("0" + (todaysDate.getDate()+3)).slice(-2); // DD
var minDate = (year +"-"+ month +"-"+ day); // Results in "YYYY-MM-DD" for today's date
// Now to set the MIN date value for the calendar to be today's date
document.getElementById("dateField").setAttribute("min", minDate);
<input type="date" id="dateField">
I am trying to set the disable the 3 upcoming days in in HTML date picker with a gap of today to 3 days. I am able to achieve this except if its 30th of March today, it has to go for the next month. in that case all dates are enabled. if its 25th of march it works fine. the upcoming dates are disabled. Where I am making the mistake. My code snippet is below.
var todaysDate = new Date(); // Gets today's date
// Max date attribute is in "YYYY-MM-DD". Need to format today's date accordingly
var year = todaysDate.getFullYear(); // YYYY
var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2); // MM
var day = ("0" + (todaysDate.getDate()+3)).slice(-2); // DD
var minDate = (year +"-"+ month +"-"+ day); // Results in "YYYY-MM-DD" for today's date
document.getElementById("dateField").setAttribute("min", minDate);
Also, is it possible just in case if I want to disable weekends from the upcoming three days? Thanks very much.
Edited as per Tiago's Answer
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = new Date();
console.log(date.addDays(3));
var dist = date.addDays(3)
var year = dist.getFullYear();
var month = dist.getMonth() + 1;
var day = dist.getDate();
var distdate = (year +'/'+ month +'/'+ day);
console.log(distdate);
document.getElementById("dateField").setAttribute("min", distdate );
Now I am getting the exact date when the date is added except it is not getting set as min.