Verify if the dates align in javascript

I am currently in the process of developing a custom ASP.Net validator that verifies whether the input data corresponds to a non-working day.

For the purpose of this validation, a "non-working day" is defined as follows:

  1. The date falls on either a Saturday or Sunday
  2. The date is considered a national holiday

I have written the necessary C# code and set up expando attributes for the error message and a list of holiday dates. In addition, I have created a JavaScript function containing the logic to identify non-working days. To streamline the example, I have omitted other sections of the code.

if (sender.NonWorkingDayErrorMessage && sender.Holidays) {

    // Check if it's a weekend
    if (date.getDay() == 6 || date.getDay() == 0) {
        sender.innerHTML = sender.NonWorkingDayErrorMessage;
        return;
    }

    // Check if it's a holiday
    var holidays = sender.Holidays.split(";");

    for (var i = 0; i < holidays.length; i++) {

        var h = new Date(Date.parse(holidays[i]));

        if (h === date) {
            sender.innerHTML = sender.NonWorkingDayErrorMessage;
            return;
        }
    }
}

However, I am facing an issue where the comparison h === date always returns false. When I added an alert and inputted the date '26/8/2013', here is the output I received:

Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => false

Even though I parsed the holidays correctly, the dates do not match. At the beginning of the function, I format the input 'date' like this:

// Split the string and reconstruct it as a date.
var parts = args.Value.split("/");
var day = parseInt(parts[0],10);
var month = parseInt(parts[1],10) -1;
var year = parseInt(parts[2],10);
var date = new Date(year, month, day);

// Validate the input date format
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
    sender.innerHTML = sender.InvalidErrorMessage;
    return;
}

If anyone has any insights into why these two dates are not recognized as matches, I would greatly appreciate your input.

Answer №1

You can attempt it in this way

let date1 = new Date(2013, 12, 1);
let date2 = new Date(2013, 12, 1);

console.log(date1.getTime() === date2.getTime());

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

Setting up Visual Studio Code for debugging HTML and JavaScript code

Struggling to troubleshoot some HTML/JavaScript using VSCode. After installing the Chrome debugger extension and configuring the launch.json as follows: { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of ex ...

Displaying a date picker within a fragment nested inside another fragment

Encountering a slight issue with implementing a Date picker using a fragment into another fragment. Upon clicking the button to load the Date picker, an error message is logged: java.lang.ClassCastException: com.example.buzzamaid.HomeActivity cannot be ca ...

Using jQuery, upon the page loading, the script will automatically trigger a

I am attempting to create an HTML file that automatically logs into my bank account. Below is the code I currently have: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> ...

What could be causing the syntax error I'm encountering while trying to extract data from this JSON file?

I've been encountering a syntax error in my console that reads "Uncaught SyntaxError: Unexpected token '}'. I've checked my JavaScript code, but being new to coding, I'm not sure why this is happening. Could the error be on the JSO ...

Using res.sendfile in a Node Express server and sending additional data along with the file

Can a Node.JS application redirect to an HTML file using the res.sendFile method from express and include JSON data in the process? ...

Issue with AngularJS binding not updating when the initial value is null and then changed

I am encountering an issue with my binding not updating, and I have a hypothesis on why it's occurring, but I'm unsure about how to resolve it. Within my controller, there is a company object that includes a property called user, which may or ma ...

Multiple CSS styles are being employed simultaneously to render the webpage

Currently, I am utilizing JavaScript to choose different CSS styles for various accessibility options such as black on white text and larger text. The issue I am facing arises when switching the CSS sheet, as elements from previously selected sheets are st ...

What is the fewest amount of commands needed to generate a client-side Javascript code that is ready for use?

In the realm of JavaScript libraries found on Github, it has become increasingly challenging to integrate them directly into client-side projects with a simple script tag: <script src="thelibrary.js"></script> The issue arises from the browse ...

How can Angular incorporate JSON array values into the current scope?

I am currently working on pushing JSON data into the scope to add to a list without reloading the page. I am using the Ionic framework and infinite scroll feature. Can someone please point out what I am doing wrong and help me figure out how to append new ...

Leveraging random attributes in Next.js without encountering the "server/client mismatch" issue

Is there a way to generate unique IDs for form inputs dynamically, in order to avoid conflicts with other elements that share the same ID? If multiple login forms are present on a single page, each with an 'email' field, setting the id property b ...

Generate a menu submenu allowing for interactive toggling and dynamic visualization of expandable/collapsible sections upon clicking, featuring visually

Looking for assistance in creating a dynamic menu with sub-menus that can expand upon clicking. The goal is to have an expandable menu structure where the user can click to expand or collapse certain sections. However, my current code is not functioning co ...

Struggling to adjust the dimensions of a React Barcode to the specified width and height?

I've encountered an issue with resizing the React Barcode Image. I prefer all barcodes to have a width of 200px and a height of 100px. From what I understand, the width of the barcode will expand when the length value increases. Below is my current co ...

Sending multiple arguments to a Vuex action

In the Vue Component code snippet below, I have a method: loadMaintenances (query = {}) { this.getContractorMaintenances(this.urlWithPage, query).then((response) => { this.lastPage = response.data.meta.last_page }) } I am trying to pass the par ...

Issue with Canvas Update in Loop while Using Three.js Library

In the code snippet provided, it takes in a number of scans which is always set to 2880. The canvas is divided into 2880 sectors to cover a full 360°. The code runs a loop from 0 to 2880, rendering colored points in each sector from the center of the canv ...

In search of the mean value using PHP, jQuery, and AJAX technologies

I have successfully created a star rating system using PHP and jQuery. The issue arises when attempting to display the average rate for a specific item that I am rating; instead, the average value printed is for all items being rated. Here is my jQuery co ...

Interactive mobile navigation featuring clickable elements within dropdown menus

I recently implemented a mobile nav menu based on instructions from a YouTube tutorial that I found here. Everything was working perfectly until I encountered an issue on the 'reviews list' page. The dropdown in the mobile nav is supposed to be ...

Unsupported server component type: undefined in Next.js version 13 is not recognized by the server

Encountered some unusual behavior in Next.js 13 while attempting a simple action. I have provided a basic example demonstrating the issue. Seeking assistance in understanding why this is occurring. This scenario involves 3 components: page: import {Conta ...

Dynamic horizontal scrolling

I need help implementing a site using React that scrolls horizontally. I'm unsure how to implement certain aspects, so I am looking for some assistance. Within a wrapper, I have multiple container Divs. <div class="wrapper"> <div class=" ...

Tips for transferring data from a module.export function to an object

I am working on a Node/Express app and facing an issue with passing data from a JavaScript function to a Jade template. The JavaScript function in question is as follows: module.exports = { getFeatures: function() { var request = require("request") ...

Customizing valueAxis dynamically in Amcharts

I am currently utilizing Amcharts to display my data. Within my chart, I have 4 graphs with valueAxes ranging from 0 to 100. Is there a method for me to dynamically change the valueAxes to a range of 0-250 after the chart has been loaded in the view? I ...