Exploring methods to deactivate specific dates within the angular material datepicker

Is it possible to prevent specific dates from being selected based on the current date? For example, if today is 8/1/16, I would like to disable the next 4 days (any number of days could be chosen), excluding weekends.

So, if today is 8/1/16, I would want to prevent selection of 8/2/16, 8/3/16, 8/4/16, and 8/5/16. Only dates after 8/8/16 would be available for selection.

Currently, I only know how to disable all weekend days in the calendar using the following filter:

$scope.disableSelectedDays = function () {

                //Creating a new moment instance to always have the current date
                var momentJS = new moment();

                if (momentJS <= moment().add(4, 'days')) {
                    return false;
                } else {
                    //Disabling only weekends
                    var day = date.getDay();
                    return day === 1 || day === 2 || day === 3 || day === 4 || day === 5;
                }
            }

This is my html:

<div class="form-group">
                                <label>Delivery date:</label>
                                <md-datepicker ng-model="myDate" md-placeholder="Enter date"
                                               md-date-filter="disableSelectedDays">
                                </md-datepicker>
                            </div>

These are the versions I am currently using:

angular-maeria: v1.1.0-rc.5 datepicker: datepicker from angular material v1.1.0-rc4-master-88f2e3f

EDIT 1

I tried implementing the changes you suggested, but now all dates in the datepicker are disabled and I can't select anything. What am I doing wrong?

Additionally, I have added the following libraries:

moment.js version: 2.14.1 angular-moment https://github.com/urish/angular-moment

Answer №1

If you're looking to implement this solution, you can utilize moment.js for your needs.

To prevent selection of dates that fall after a certain number of days from the chosen date, you can follow this code snippet (Please note that this is a theoretical example and has not been tested, so there could be potential errors).

// Define the date you wish to validate

// Define the number of days as 4 in this case

$scope.disableSelectedDays = function (date) {
    if(date <= moment().add(4, 'days')) {
        return false;
    } else {
        // Disable weekends only
        var day = date.getDay();
        return day === 1 || day === 2 || day === 3 || day === 4 || day === 5;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for preventing a table from showing up while scrolling unnecessarily when utilizing a heading with the CSS position property set to 'sticky'

Currently, I am facing an issue with creating a sticky header for my table. The problem arises when the header of the table has rounded edges (top right and top left) along with a box-shadow applied to the entire table. As the user scrolls through the tabl ...

Guide on retrieving just the time from an ISO date format using JavaScript

let isoDate = '2018-01-01T18:00:00Z'; My goal is to extract the time of 18:00 from the given ISO date using any available method, including moment.js. ...

Using AngularJS to encapsulate the JSON response received from the server

Currently, I have a basic CRUD application that is operational. However, I am looking to enhance every response received from the server by adding two additional parameters: 'error' => boolean, 'errorMessage' => string, 'dat ...

What is the reason behind Angular Material's decision to apply background colors?

Switching over to Angular Material from Bootstrap has brought up some unexpected issues. I decided to use the md-datepicker library, but noticed that it was applying a background color to the picker: md-datepicker.md-default-theme, md-datepicker { bac ...

Integrating tilt and zoom functionality on a web page based on mouse movements

While browsing through some messages on chat, I came across something interesting: I noticed that as I moved my mouse left or right, the content would tilt in that direction, and when moving up and down, the content would zoom in or out. It was a really c ...

Is there a way to manually stop a file upload (stream) using a XMLHttpRequest on the server?

Utilizing XMLHttpRequest (Level 2) to transfer a file to a node.js server, I am currently examining the file-stream for valid headers on the server side. The goal now is to halt the upload if any errors are encountered during streaming. My XMLHttpRequest c ...

"Integrating JavaScript in C# Code Behind: A Step-by-Step Guide

Is there a way to trigger this javascript function using C# code in the backend when the page loads? Your assistance is greatly appreciated, thank you. <script type="text/javascript"> document.onkeydown = function (event) { event = ( ...

Can we use ToggleClass to animate elements using jQuery?

I have a section on my website where I want to implement an expand feature. I am currently using jQuery's toggleClass function to achieve this effect. jQuery('.menux').click(function() { jQuery(this).toggleClass('is-active&a ...

Looking for a solution to split barcode data across two text fields

I am looking to enhance my data input process by utilizing a barcode scanner in conjunction with JQuery. The barcode scanner I have reads both the serial number and model name of each product, but currently displays them together in one text field. Is ther ...

Align the rotation of Object3D at the tab level

Seeking assistance, I am attempting to synchronize the rotation of an object loaded in the active browser tab with another object loaded in a second tab. The idea is that when the Object in the active browser tab rotates, it will send a message to the Obj ...

Express framework in NodeJS encounters an undefined header error

Utilizing Microsoft Flow to dispatch an HTTP POST request to my server, the request body includes a custom header known as "Email-To" with a string value. Here is the body: { "$content-type": "multipart/form-data", "$multipart": [ { "headers ...

What is the JavaScript mouseenter/out event all about?

My goal is to create a hover effect using JavaScript. I am utilizing the mouseenter and mouseout events to display the content when the mouse hovers over a button and remove the content when the mouse moves out. However, I am facing an issue where the cont ...

Why using $refs in interpolation fails and leads to errors in Vue.js 2.x components

Here is a codepen I created: codepen const sidebar = { name: "sidebar", template: "<p>SIDEBAR</p>", data() { return { active: true }; }, methods: { test() { alert("test: " + this.active) } } }; new Vue ...

Divide a JavaScript project into multiple packages using either webpack or npm

I am embarking on the development of an application that needs to be compatible with Windows (PC), Android, and iOS. To achieve this, I plan to use Electron for Windows and React Native for mobile platforms. Both applications will be built using React and ...

Using Telerik Grid in MVC to access the object that is passed to ClientEvent functions on the Javascript side

Here's a challenging question I have. I'm working with a Telerik Grid that has some ClientSide Events: .ClientEvents(events => events .OnDataBinding("SetAjaxParameter") ) Within the SetAjaxParameter function, I am setting parameters for ...

JavaScript's async function has the capability to halt execution on its own accord

Presented here is a JavaScript async function designed to populate a sudoku board with numbers, essentially solving the puzzle. To enhance the user experience and showcase the recursion and backtracking algorithm in action, a sleeper function is utilized b ...

Mui CardContent not displaying transparent background color properly

I recently completed a course that used MUI v4, and I'm now facing challenges with transitioning to v5. Despite my best efforts, I am struggling to replicate all the styles and find myself stuck. This issue relates to my FeaturedMovie.jsx component i ...

Is it possible to display a subtle grey suggestion within an HTML input field using only CSS?

Have you ever noticed those text input boxes on websites where a grey label is displayed inside the box, but disappears once you start typing? This page has one too: the "Title" field works the same way. Now, let's address some questions: Is ther ...

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

Is there a way to store a file in a server directory using the <a href='mypdf.pdf'> tag?

I find myself in a bit of a bind. I have a document that needs to be saved in my server directory, which is attached in an <a href='mypdf.pdf'>My pdf</a> tag. The issue is that the filename, mypdf.pdf, is dynamically changing via Jav ...