Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Weekends on HTML 5 input type date

Is it possible to disable all weekends for HTML 5 input type date?

<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />
like image 404
kaplievabell Avatar asked Apr 16 '18 17:04

kaplievabell


People also ask

How do I restrict past dates in HTML5 input type date?

How do I turn off past dates? the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.

How do I restrict date format in HTML?

If the value of the max attribute isn't a possible date string in the format yyyy-mm-dd , then the element has no maximum date value. If both the max and min attributes are set, this value must be a date string later than or equal to the one in the min attribute.

How do I set the calendar to not allow dates before today's date in HTML?

There is no possible way to do this with pure HTML5. and; var today = new Date(). toISOString().

How do I make my date field Disabled?

< p >To disable the date field, double click the "Disable" button.


2 Answers

Just check the day. If it's a weekend you can reset the value and tell the user to pick something else.

const picker = document.getElementById('date1');
picker.addEventListener('input', function(e){
  var day = new Date(this.value).getUTCDay();
  if([6,0].includes(day)){
    e.preventDefault();
    this.value = '';
    alert('Weekends not allowed');
  }
});
<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />
like image 176
I wrestled a bear once. Avatar answered Sep 18 '22 11:09

I wrestled a bear once.


Just for HTML 5 input type date this isn't possible.
You can use flatpickr as an alternative: ( https://chmln.github.io/flatpickr/examples/#disabling-specific-dates )
It looks better and also can do what you are asking for!
In the documentation is mentioned a property named disable that you can use to remove weekends.

{
"disable": [
    function(date) {
        return (date.getDay() === 0 || date.getDay() === 6);
    }
],
"locale": {
    "firstDayOfWeek": 1 // start week on Monday
}

}

Full working example:

$("#date1").flatpickr({
    enableTime: true,
    dateFormat: "m-d-Y",
    "disable": [
        function(date) {
           return (date.getDay() === 0 || date.getDay() === 6);  // disable weekends
        }
    ],
    "locale": {
        "firstDayOfWeek": 1 // set start day of week to Monday
    }
});
<html>
  <head>
    <title>Using Flatpickr</title>
  <!--  Flatpicker Styles  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/themes/dark.css">
  </head>
  <body>
      <input id="date1"  placeholder="MM/DD/YYYY" data-input />
        <!-- jQuery -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <!--  Flatpickr  -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.js"></script>
  </body>
</html>
like image 21
Jbadminton Avatar answered Sep 19 '22 11:09

Jbadminton