What is the process for deactivating a range of dates?

I am trying to prevent users from selecting dates within the range specified by l1 and l2. However, my current method only disables the date 13.

var l1 = new Date("2019-07-13");
var l2 = new Date("2019-07-30");
this.flag = 0;

  this.filter2 = function(date) {
        var c = date.getDate();
        for(i=l1.getDate();i<=l2.getDate();i++)
        {
            return c != i;
            continue;
        }
  }

Answer №1

Timestamps are effective, but it's also important to take my comments into consideration

var start = new Date("2019-07-13");
var end = new Date("2019-07-30"); 
// The date format for end will include the time as well, so if you want it to be strictly for 2019-08-01, use this date directly or set end to new Date("2019-07-30 23:59:59.999")

// This method will return true if the date value provided to the function is outside the specified range
// For example, dates like: 2019-07-12, keep in mind that it will work for today's date due to the getTime method considering hours, minutes, seconds, and milliseconds 
this.filter2 = function(date) {
  return start.getTime() < end.getTime() && (date.getTime() < start.getTime() || date.getTime() > end.getTime())
}

console.log(filter2(new Date())) // In the case of today, based on the information mentioned above, it will return true
console.log(filter2(new Date('2019-07-15'))) // false
console.log(filter2(new Date('2019-07-12'))) // true

Answer №2

To determine if a specific date falls within a certain range, you can compare the timestamps of the date with two reference dates, l1 and l2.

var l1 = new Date("2019-07-30");
var l2 = new Date("2019-07-13");

function checkDate(date) {
  if (date instanceof Date && l1 instanceof Date && l2 instanceof Date) {
      return l1.getTime() < l2.getTime() ? !(date.getTime() >= l1.getTime() && date.getTime() <= l2.getTime()) : !(date.getTime() >= l2.getTime() && date.getTime() <= l1.getTime())
    } else {
      return false;
    }
}

console.log(checkDate(new Date("2019-07-13")));
console.log(checkDate(new Date("2019-07-12")));

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

Set up the AngularJS and NodeJS server for optimal performance

Hey there! I am currently working on developing a RESTful API using Node.js and Express, while also planning to build a client-side application with AngularJS. According to the AngularJS official website: "The angular-phonecat project comes with a basic s ...

Obtaining a compressed file via a specified route in an express API and react interface

Feeling completely bewildered at this point. I've had some wins and losses, but can't seem to get this to work. Essentially, I'm creating a zip file stored in a folder structure based on uploadRequestIds - all good so far. Still new to Node, ...

Show the loading icon once to indicate that the page is waiting for an AJAX call

I am currently setting up a table that is refreshed with new data every 4 seconds using AJAX. During the initial page load, I would like to show a loading message while waiting for the AJAX to complete. Currently, I have successfully displayed the loading ...

Incorporate a Fadein effect into the current CSS for mouseover link interaction

Is there a way to enhance my current css code for a mouseover link with a background image by adding a fade in and out effect? What's the most effective method to achieve this using jquery? .sendB { width: 100%; height: 125px; background: ...

What are some effective strategies for reducing excessive re-rendering of React components?

Here is how I am displaying a list of components on the screen: const MessagesContainer = ({ messages, categories, addHandler }) => { const options = categories.map(category => ( { value: category.name, label: category.name } )); ...

What benefits come from employing jQuery for data storage techniques?

After learning more about jQuery data storage, I have a question. Is there any advantage to using either of these methods: $('#editCity').data('href', "xx"); var a = $('#editCity').data('href'); or $('#edit ...

What is the best way to duplicate a value and save it in an array?

I have a calendar and I want to be able to copy the selected day's value and store it in an array. Then, if another day is clicked, I would like to add the current day's value along with the previous day's value to the array. Users should be ...

After changing the value of a <select> element in Vue, the fetched data is delayed by one step

I'm currently working on a feature that involves fetching data from a URL that changes every time a user chooses a new value from a <select> dropdown. The fetched data updates the songkickData array with the latest information. However, when I c ...

Adding to the beginning of a list in JQuery mobile

Having trouble figuring out how to prepend my list in jQuery mobile while keeping the divider on top of the most recent item added. I attempted to prepend the newly added item, but it ended up shifting the divider to the bottom instead. function loadScan ...

What is the best way to set up Flow type checking for functions passed as props in a React and Redux application?

In my app, I've been passing Redux action creators as props and want to improve type checking. Using the generic Function type has limitations, so I tried using the existential operator (*) in my Props type with no success in getting Flow to infer the ...

Tips for displaying dependent dropdown options in AngularJS

I need assistance with creating a form that includes two select boxes. Depending on the option selected in one select box, specific options should be shown or hidden in the other select box. For example: Select box A Options: Apple Banana Select Box B O ...

Creating a Next.js application that retrieves mock data and dynamically presents it on the user interface

I've been attempting to retrieve some placeholder data from an API and showcase it on the screen, but unfortunately nothing is appearing. Interestingly, the data does show up in the console, just not on the actual screen. function Shop() { const [pr ...

Scroll down 1 page using Javascript and Jquery

Hey there! I'm looking to create a website where a small scroll on the mouse wheel will take the site to the next anchor. It's kind of hard to explain, but one good example is the Tesla Model 3 website on the US site here. Does anyone have any t ...

Meteor Routing Issue: The path "/"" you are trying to access does not exist in the routing system

After upgrading Meteor to version 1.3.2.4, I encountered an issue where the error message "Error : There is no route for the path: /" appeared. I made sure to update all packages to their latest versions as well. I tested the application in both "meteor" ...

Be mindful of potential missing dependencies when utilizing event listeners and states within useEffect

Whenever I utilize the addEventListener() method alongside trying to access some state within the useEffect, I consistently face the same issue. Adding the state as a dependency is not an option, as that would result in multiple event listeners being creat ...

Having trouble getting my asynchronous promise to work properly

I am currently working on implementing a login server function and I am struggling to understand why the promise I'm making is not being called. My setup involves using MongoDB with Mongoose as the backend, which is connected to using User.findOne. A ...

What is the best way to scale down my entire webpage to 65%?

Everything looks great on my 1920x1080 monitor, but when I switch to a 1024x768 monitor, the content on my webpage becomes too large. I've been manually resizing with CTRL+Scroll to reduce it to 65%, which works fine. Is there a code solution using CS ...

Array data causes tabs to be shown incorrectly

My attempt to create tabs similar to those in this tutorial has hit a snag. While I can easily display hard coded tabs, I'm facing issues when trying to populate the tabs from a list as they end up being displayed incorrectly. Here is the code and im ...

JavaScript Function to Retrieve URL Parameter in PHPIn this tutorial

I'm currently working on an HTML5 game where each level has its own dedicated HTML page. The results of each level are saved in a JavaScript variable. My question is, how can I pass this information to the next page using the URL? I was considering ...

Basic Search tool, displaying a <div>

Hey there, I've been searching for a solution for a while now and haven't found one yet. So here's my question: I created a simple website to display random recipes for those times when you're not sure what to make for dinner. I also w ...