Issues arising when using December in a JavaScript function to determine weekends

My function is used to determine if a day falls on the weekend or not, and it works perfectly for most months except December. What could be causing this issue?

 weekEnd: function(date) {
      // Determine if it's a weekend
      var date1 = new Date(date);
      var day = date1.getDay();
      var result = 1;

      if (day === 6 || day === 0) {
        result = 0;
      }
      return result;
    },

Answer №1

Unfortunately, I am unable to replicate the issue as it is working perfectly for me. Instead of using 0 or 1, try using boolean values true or false.

If you are experiencing this problem, it could be due to an incorrect date format being used.

Based on your comment, it seems like you may be French and the Date object might be set to MM-DD-YYYY format instead of the correct DD-MM-YYYY format.

Below is the code that works for me, even in December:

const isWeekend = (date) => {
    const day = new Date(date).getDay()
    return ( day == 6 || day == 0)
}

// 12-15-2019 --> Sunday 15th December 2019
console.log(isWeekend('12-15-2019')) // returns true

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

Unveiling the Magic: Transforming Objects into HTML Attributes using AngularJS

I have been working on developing a highly dynamic ng directive that creates tables based on a given data array and configuration object. I am looking for a way to assign attributes dynamically based on an object within the scope. For example, if I have an ...

Top tips for handling HTML data in JSON format

I'm looking to fetch HTML content via JSON and I'm wondering if my current method is the most efficient... Here's a sample of what I'm doing: jsonRequest = [ { "id": "123", "template": '<div class=\"container\"&g ...

Generate a custom JavaScript file designed for compatibility with multiple servers

I am looking to develop a JavaScript file that can be easily added to other websites, allowing them to access functions within my database using an API similar to the Facebook like button. This API should display the total likes and show which friends have ...

Discover the smallest value in real-time during ng-repeatiteration

In my HTML code, I am utilizing the ng-repeat function. When the user clicks on addBox(), a new input box is appended for adding new data: <div class="col-md-6 col-md-offset-2"> <h5 class="over-title">Variant</h5> <a class="btn ...

JQuery: Issues with attaching .on handlers to dynamically added elements

I'm currently developing a comment system. Upon loading the page, users will see a box to create a new comment, along with existing comments that have reply buttons. Clicking on a reply button will duplicate and add the comment text box like this: $( ...

Ever wonder what goes on behind the scenes when you press a button before the Javascript method is carried out

My web application is built using ASP.NET MVC 4, jQuery, and Telerik's Kendo controls. Within my webpage, I have the following code snippet: <input id="cmdSaveToFile" title="Save To File" class="button" type="button" value="Save To File" onclick= ...

Modify the values of an object by utilizing the setter function

How can I utilize the setter method to update existing values of an object and perform mathematical operations? var obj = { set model(object) { //method's logic } }; obj = {x:10, y: 20, p: 15}; obj = { x:10, y: 20, p: 15 set mod ...

Changing the Month Label Format from Short (MMM) to Long (MMMM) in Angular Material Datepicker

I am looking to customize the month labels in Angular Material datepicker. By default, the month view displays in MMM format and I want to change it to MMMM using custom MatDateFormats. export const APP_DATE_FORMATS: MatDateFormats = { parse: { dat ...

How can I update the state with the value of a grouped TextField in React?

Currently working on a website using React, I have created a component with grouped Textfields. However, I am facing difficulty in setting the value of these Textfields to the state object. The required format for the state should be: state:{products:[{},{ ...

Delete ObjectId from Array using Node and Mongoose

Currently, I am working on a website that features a comments section for campsites. This platform is similar to Yelp but focuses on reviewing campsites. Each campsite in the MongoDB collection has a field called "comments" which stores the IDs of all comm ...

JavaScript - issue with event relatedTarget not functioning properly when using onClick

I encountered an issue while using event.relatedTarget for onClick events, as it gives an error, but surprisingly works fine for onMouseout. Below is the code snippet causing the problem: <html> <head> <style type="text/css"> ...

What is the process by which React loads and runs JSX content?

What is the method used to identify and handle JSX code? <script src="src/main.js" type="text/babel"></script> ...

Is it recommended to use django's render_to_string response with innerHTML?

This is my first Django project, so please bear with me if I'm making some basic mistakes. I'm currently working on a webpage that will display a report in an HTML table. However, I feel like the process I'm using to gather the data and cons ...

Get the data from the files in the request using request.files in Node.js

Is there a way to read the content of a file (either a txt or CSV file) that a user uploads without saving it to local storage? I know I can save the file in an upload directory and then read it from storage. However, I'm wondering if there is a way ...

Does anyone know of a CSS/JavaScript framework that can be used to replicate the look and functionality of the iOS home

I'm wondering if there is a CSS/JavaScript framework that replicates the layout of an iOS home screen. I want to create a kiosk website with a grid layout similar to Android/iOS where apps can be displayed as icons. ...

Refreshing the page causes JavaScript to fail loading

Recently, I encountered a puzzling error. Upon visiting this link The carousel fails to load properly near the bottom of the page. However, if you click on the logo or navigate back to the home page, it works fine. Additionally, performing a command + r ...

Establishing context boundaries in Angular

Having my variables bound to this in the controller and using controllerAs: 'game' in the route designation allows me to include them in the HTML using {{game.var}}. At times, I find myself binding objects that I want to display, which leads to r ...

Error encountered while parsing Japanese characters using the express body-parser resulting in a bad control character issue

Currently, I am sending a large JSON string to a node express endpoint that is set up like this: import bodyParser from 'body-parser'; const app = express(); const jsonParser = bodyParser.json({ limit: '4mb' }); const databaseUri = &ap ...

Are there any concerns about memory leaks when using recursive JavaScript Objects?

Our model entities are passed through the API in JSON format, allowing us to inflate them client-side for use on both the server and client sides. These entities have standard Hibernate bi-directional relationships. As you navigate through an object in the ...

Looking to change the font text color in the filter section of the react-data-table-component-extensions?

Can anyone offer some assistance with this issue? I am currently using react-data-table-component along with react-data-table-component-extensions. I have also imported the index.css for the component-extension. import DataTable from "react-data-tabl ...