1

 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.

Roshan Zaid
  • 336
  • 1
  • 4
  • 21

2 Answers2

1

Here's how I did it.

I converted the extra dates into milliseconds, added it to today's date and made a new Date Instance from that.

You should learn about padStart. It fills up the string to the required length 1st argument, with whatever is passed into the 2nd argument

const addDate = 3; //Converted these extra days to milliseconds and added it to today's date
const min = new Date(Date.now() +  (addDate * (24 * 60 * 60 * 1000)));
const year = min.getFullYear();
const month = (min.getMonth() + 1).toString().padStart(2, '0');
const day = min.getDate().toString().padStart(2, '0');

const 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);
document.getElementById("minText").innerText = `Min Date: ${minDate}`;
input[type=date]:valid {
  border-color: green;
}

input[type=date]:invalid {
  border-color: red;
}
<input type="date" id="dateField" required>
<span id="minText"></span>
a.mola
  • 3,883
  • 7
  • 23
0

var day = ("0" + (todaysDate.getDate()+3)).slice(-2); returns 34 if today is 30th Mar

In this case, you can use setDate function of Date object to calculate the minDate value:

var minDate = new Date(); // Gets today's date
minDate.setDate(minDate.getDate() + 3); // right here
// update min setting
document.getElementById("dateField").min = todaysDate.toISOString();
hoangdv
  • 15,138
  • 4
  • 27
  • 48