Prevent selection of specific weekdays in Javascript

Looking to enhance my calendar functionality in JavaScript by disabling multiple week days. I have successfully disabled Sunday using the code snippet below, which utilizes the getDay() method. Any guidance on how to disable more than one week day with JavaScript?

disabled: function (date) {
  if (date.getDay() === 0) {
    return true;
  }
  else {
    return false;
  }
}

Seeking assistance.

Answer №1

If you need to disable multiple days, you can create an array and check if the day is in the array:

 disabled: function (date) {
   return [0, 6].includes(date.getDay());
 }

Alternatively, you could try a more complex method involving bitshifting (just kidding :)):

 disabled: function (date) {
   return 1 & ( 0b1000001 >> date.getDay());
 }

On a different note: If you want to disable dates prior to today as well, you can make a simple comparison:

  disabled: function (date) {
   return (
      [0, 6].includes(date.getDay()) ||
      date < new Date
   );
 }

Answer №2

Utilize either condition

 if (date.getDay() === 0 || date.getDay() === 6) 

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

"Unraveling the Mystery: In Javascript Vue, what is the secret behind the origin of the variable 'e' found in the function

Where does the mysterious variable e in onFileChange(e) come from? In the code snippet below, we see a variable e being used in onFileChange(e). However, this variable is not declared or imported anywhere in the code. How is it possible for this to be val ...

Why isn't Google Map showing up in my HTML <div> section?

I am currently working on a web page that consists of two containers. The mainmapdiv container covers the entire page, while the mainhomediv container is positioned on top of the mainmapdiv. My goal is to hide the mainhomediv container and show a Google Ma ...

Is it necessary to release an NPM package when creating a custom Javascript library for local use?

I want to organize my javascript utility functions in a central folder and access them from multiple projects. It appears that I am unable to import functions from outside the src file of my project. Do I need to create an NPM package? Must I duplicate t ...

How to display an array with JSON objects in Angular 4

Looking to display specific data from an array in my .html file that originates from my .ts file: myArray: ["03/05/2018", "2:54", "xoxo", "briefing", "your", [{ "Id": "1", "Time": "20:54", "Topic": "mmmmm", "GUEST1": { "Role": "HS" ...

The jQuery click event not triggering on ASP.NET control

I am currently developing a C#/Asp application that utilizes jQuery. I am trying to implement a specific function that should be called every time a click event is triggered on an element with a class of "loadingInProgress". However, the code snippet belo ...

Developing typeScript code that can be easily translated and optimized for various web browsers

Can TypeScript alleviate the worry of having to use code such as this (especially when considering browsers like IE that may not support indexOf)? arrValues.indexOf('Sam') > -1 Does the transpiling process in TypeScript generate JavaScript c ...

Angular 9: Issues with [innerHTML] not displaying unclean values, even for simple strings

I am facing an issue with two angular 9 apps in monospace font. In one of the apps, the code snippet below does not display any text on the browser document: Component: myText = "some text" Template: <p [innerHtml]="myText"><p> Even if I ...

Switching the keyboard language on the client side of programming languages

I'm interested in altering the keyboard language when an input element changes. Is it possible to modify the keyboard language using client-side programming languages? And specifically, can JavaScript be used to change the keyboard language? ...

Javascript can determine the percentage values of the top and left positions

I am currently working on a project that involves capturing mouse clicks within a square region and adding a new div at the clicked spot. However, I need to convert the Xpx and Ypx coordinates into percentages using JavaScript because the size of the squar ...

Error encountered when extending Typography variant in TypeScript with Material UI v5: "No overload matches this call"

Currently, I am in the process of setting up a base for an application using Material UI v5 and TypeScript. My goal is to enhance the Material UI theme by adding some custom properties alongside the default ones already available. The configuration in my ...

Issue encountered when toggling flip switch in Jquery Mobile

I am trying to create a calculator for my app using Jquery Mobile, but I am facing an issue with the flip toggle switch. Here is my script: <script type="text/javascript"> function dosis() { $peso = $('#peso').get(0).value $dosis = ...

unorthodox method of transmitting parameters within express.js source code

While searching for the source code of express router, I came across this interesting piece: var debug = require('debug')('express:router:route'); I am curious to know more about this unusual way of passing arguments. Can someone ...

What is the best way to automatically clear an input field after a specified delay?

On my landing page, I have a single input field where users can enter their email address. Upon successful entry... success: function(result){ console.log(result.status); if(result.status == true) { $('input').attr("style", "color:green" ...

Vue.js2 - Detection of Observer in Array

A question for beginners in vue.js. I am trying to display data using the CanvasJS Library that is received via websocket. Everything works fine with the data until I introduce vue components into the mix. Let me clarify: export default { data() { r ...

Obtaining fresh data during an ajax update

I've noticed that my code is functioning correctly, but I'm having trouble grasping it completely. Could someone please provide an explanation? Here is the code snippet: <script type="text/javascript"> var portletNamespace = '#& ...

Capture the item selected from the dropdown using ng-model to retrieve the name instead of the

I need help getting the name/text/html of the selected option in a dropdown using angular.js, rather than just its value. Currently, I have this setup which retrieves the value: Here is my HTML code snippet <select class="SelectCar" ng-model="Selected ...

Can anyone tell me the best way to access the name attribute of an HTML element in TypeScript?

Currently, my code is utilizing the name attribute to verify whether the user has entered information in a specific field and validating the input. However, I am facing an issue where the submit button remains active even if there are empty fields presen ...

Utilize jQuery and JSP (or PHP) to showcase detailed information about a specific item when a user clicks on it within an HTML page

For my upcoming college project, I am tasked with developing a movie library. The main page of the library will feature various movie posters. Upon clicking on a poster, users will be directed to a dedicated page displaying detailed information about the ...

What are some ways to restrict dynamic form submissions?

$(document).ready(function(){ var i = 1; $('#add').click(function(){ if(i < 5){ i++; $('#dynamic_field').append('<tr id="row'+i+'"><td><div class="form-group">& ...

What is the syntax for accessing a nested object within the .find method?

Currently building an application in node.js. I am struggling with referencing the "email" element in the "userData" object within the Order model when using the find method. Any suggestions on how to properly refer to it? Order model: const orderSchema = ...