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

What is the best way to showcase a half star rating in my custom angular star rating example?

component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { projectRating ...

Exploring the nuances of various browsers in JavaScript

I am facing an issue where my JavaScript code functions correctly in Internet Explorer, but not in Firefox or Safari. I have a loop that goes through each element and, depending on the variable inside a text box, triggers an alert message. The code snippet ...

"Learn how to trigger an event from a component loop up to the main parent in Angular 5

I have created the following code to loop through components and display their children: parent.component.ts tree = [ { id: 1, name: 'test 1' }, { id: 2, name: 'test 2', children: [ { ...

Retrieving the maximum values from JSON data using D3

Currently, I am working with D3 and JSON data by using a function that looks like this: d3.json("http://api.json", function(jsondata) { var data = jsondata.map(function(d) { return d.number; }); After executing this, the value of the data becomes ["2", ...

Can JavaScript be used to create a CSRF token and PHP to check its validity?

For my PHP projects, I have implemented a CSRF token generation system where the token is stored in the session and then compared with the $_POST['token'] request. Now, I need to replicate this functionality for GitHub Pages. While I have found a ...

Serialize a form while keeping the submitted data private

Is there a way to achieve serialization without triggering the submit function for an ajax call? I've searched extensively for a solution to this issue without any luck. The java script function is invoked when a button within the form is clicked. do ...

When using Angular2, I have found that I am unable to extract the body data from JSONP responses. However, I have discovered that this issue

Initially, I developed the SERVER using spring-boot framework. The code for this looks like: public class App { @RequestMapping("/") @ResponseBody String home(HttpServletRequest request) { String aa=request.getParameter("callback"); System.out.pri ...

Execute HTML and JS files through Eclipse PDT to view in a web browser

Is it possible to open HTML and JS files in a web browser within Eclipse PDT? Right now, only PHP files seem to launch successfully, otherwise an "Unable to Launch" dialog pops up. Any advice is appreciated! ...

Obtain details regarding a worker's collision

This code snippet is being used to manage cluster crashes within a node application cluster.on('exit', function (worker, code, signal) { console.log("error in cluster",worker); console.log("cluster code",code); console.l ...

Using Angular ui.bootstrap tabs along with ui.router: a step-by-step guide

My initial approach to using ui.bootstrap tabs with ui.router is as follows: <tabset> <tab heading="Tab1" select="$state.go('home.tab1')"> <div ui-view="forTab1"></div> </tab> <tab heading="Tab2" select ...

The CORS problem arises only in production when using NextJS/ReactJS with Vercel, where the request is being blocked due to the absence of the 'Access-Control-Allow-Origin' header

I've encountered a CORS error while trying to call an API endpoint from a function. Strangely, the error only occurs in production on Vercel; everything works fine on localhost. The CORS error message: Access to fetch at 'https://myurl.com/api/p ...

Are there any similar events to OnRouteChange in Angular?

I'm looking to execute a function every time a route changes in Angular. Does Angular have an event similar to OnRouteChange for this purpose? ...

Using ng-repeat to iterate over an array of strings in Javascript

I am working with a JavaScript Array that contains strings: for(var i =0; i<db.length; i++) console.log(db[i]); When I run the code, the output is as follows: dbName:rf,dbStatus:true dbName:rt,dbStatus:false Now, I am trying to use ng-repeat to ...

Using the Ionic framework to transfer data from a controller variable to a page

Hey there! I'm currently working on a hybrid app using the Ionic Framework, but I believe my error lies more within Angular. Here's the code snippet that's giving me trouble: <ion-view class="back" ng-controller="webCtrl" view-title="{{ ...

ReactJS component not triggering OnChange event in IE 11

While exploring the React.js documentation, I came across a suggestion to use the onChange event for text areas. Interestingly, when I tried pasting some text into an empty IE 11 text area, the onChange event failed to trigger. Surprisingly, it worked perf ...

Creating a geographical representation using the chosen city from a dropdown menu in AngularJS

I am working on a project where I have a select box for cities that are populated using ng-options. My goal is to display a map on the website based on the city selected in the select box. Here is an example of how I am fetching city names: // JavaScr ...

Having trouble getting the toggleClass function to work properly?

I'm facing an issue with some straightforward code that I've written. $(function() { $("#tren").click(function() { $("#trens").toggleClass("show"); }); }); .show { color: "red"; } <ul> <li id="tren">Some text</li> < ...

Accessing Facebook through Login with only a button visible

I need help with checking the user's login status on Facebook. I have implemented the code provided by Facebook, but all I see is the login button. How can I determine if the user is already logged in or not? function testAPI() { console.log(&apo ...

Trigger Bootstrap Modal using ASP Razor code within MVC Model value execution

I have a web application built with ASP .NET Core and MVC. The app features a side menu that allows the user to navigate through different pages based on their workflow. For example, when a user logs in for the first time, they are directed to the Home pag ...

Loading tab content on click using jQuery

These tabs have a chrome-like appearance. When the first tab (Facebook) is clicked, it shows Facebook content. Clicking on the second tab (Twitter) displays the content of the first tab. Tabs three, four, and five show their respective contents without any ...