The following working day with Moment.js

I'm having an issue trying to retrieve the next business day with my code. Currently, it is only displaying the day immediately following. Despite looking into similar questions for a solution, I have yet to identify the error.

While this question is related, it doesn't fully address my specific concern:

How to exclude weekends between two dates using Moment.js

 $scope.businessDay = new Date();

        if (moment().day() === 5) { // It's Friday, so show Monday
            // Set to Monday
            $scope.businessDay = moment().weekday(8).format("MMMM Do YYYY");
        }
        if (moment().day() === 6) { // It's Saturday, so show Monday
            // Set to Monday
            $scope.businessDay = moment().weekday(8).format("MMMM Do YYYY");
        }
        else { // For other days, display the next day
            $scope.businessDay = moment().add('days', 1).format("MMMM Do YYYY");
        }

Answer №1

Everything is running smoothly. You just overlooked an else

    if (moment().day() === 5) { // Friday, display Monday
        // set to Monday
        $scope.businessDay=moment().weekday(8).format("MMMM Do YYYY");
--> } else if (moment().day() === 6) { // Saturday, display Monday
        // set to Monday
        $scope.businessDay=moment().weekday(8).format("MMMM Do YYYY");
    }
    else { // Other days, display the following day
        $scope.businessDay= moment().add('days', 1).format("MMMM Do YYYY");
    }

Answer №2

Here is an alternative approach that is not reliant on the initial day of the week:

function generateNextValidDate(format = 'MMMM Do YYYY') {
  let daysToAdd = 1;

  if (moment().day() === 5) {
    // set it to Monday
    daysToAdd = 3;
  } else if (moment().day() === 6) {
    // set it to Monday
    daysToAdd = 2;
  }

  return moment().add(daysToAdd, 'd').format(format);
}

Answer №3

if (moment().day() === 5 || moment().day() === 6) { // If it's Friday or Saturday, display Monday's date
    $scope.businessDay = moment().day(8).format("MMMM Do YYYY");
} else { // For other days, display the next day's date
    $scope.businessDay = moment().add('days', 1).format("MMMM Do YYYY");
}

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

Troubleshooting: npx create-react-app displaying errors during installation

Encountering an error while trying to install create-react-app using npx. Seeking assistance on resolving this issue. My Node.js version is v16.14.2 and npm version is 8.5.0 Installing packages. This may take a few minutes. Installing react, react-dom, a ...

Tips for retrieving data from a JSON array

I have a JSON object that looks like this: var obj={ "address":{ "addlin1":"", "addlin2":"" }, "name":"sam", "score":[{"maths":"ten", "science":"two", "pass":false }] } Now, when I attempt to m ...

Display input field in AngularJS when radio button is selected

My JavaScript array looks like this: $scope.quantityMachines = [ { 'snippet' : 'One' }, { 'snippet' : 'Two' }, { 'snippet' : 'Three or more', 'extraField' : true }, { ' ...

Ways to create a looping mechanism with specified number restrictions and limitations

Can anyone assist me with this problem? I am looking to create a "looping" effect similar to the image provided. What is the logic behind this repetition? Thank you in advance for your help! Here is an example output: ...

Pressing the Enter key will result in data fetched from JSON being logged multiple times

I am currently implementing a search feature on my React website. When a user enters a keyword in the search input, the keyword is matched in a JSON file. If a match is found, it logs "yes" to the console; otherwise, nothing is logged. Here is an example ...

Running a Gulp task to launch an express server

Within the same directory, I have both a server.js file and a gulpfile.js file. In the gulpfile.js, I am requiring the server.js file: var express = require('./server.js') My intention is to run it within the default task: gulp.task('defa ...

Next.js encountered an invalid src prop

Error: The src prop is invalid () on next/image, the hostname "scontent-atl3-2.xx.fbcd.net" has not been configured under images in your next.config.js For more information, visit: https://nextjs.org/docs/messages/next-image-unconfigured-host ...

Utilizing React Selector: Propagating the onChange Event Up Two Components

Struggling to pass an onChange event from React selector through two components back up to the parent App. The issue is that e.target.value is coming back as undefined, indicating that the event might not be returning properly. Can someone pinpoint what&ap ...

Tips for preloading a small placeholder image before the main content is loaded

After browsing , I noticed an interesting image loading style. The website initially shows a patterned image before revealing the actual content. This method creates visually appealing content while waiting for the entire webpage to load. Upon inspecting ...

Dynamically transcluding multiple elements in Angular

Angular 1.5 introduces the option to multi-transclude. An interesting feature would be the ability to transclude a variable number of items into a directive and specify their names and positions at a later stage (for example, in the link/compile functions ...

Move files in Node.js without removing the original source directory

Currently, I am facing an issue while attempting to transfer files from one directory to another using the mv module. The problem arises when the files are successfully moved, but the source directory is automatically deleted. My intention is for only the ...

Exploring AngularJS's capabilities with asynchronous tasks

I am in the process of developing a simple app using AngularJS. One of the key functionalities I am working on is removing items from a list. To achieve this, I have created the following code snippet: $scope.removeItem = function(item) { var toRemove = ...

Guide to showcasing a placeholder in MUI's Select component

How can I add the placeholder "Select a brand" to this select element? I've tried different options with no luck. Here is the code snippet I am working with: <FormControl fullWidth> <InputLabel id="demo-multiple-name-label" ...

Tips for automatically refreshing a page after submitting a form

I have a template with two components - a filter and a request to the API. I am wondering if it's possible to update the values of the request component after submitting the filter. On the main page, the request component has default values. When a u ...

Searching Firebase by using comparison operators on various fields

const FindFiis = async () => { const data: any[] = []; // Firebase query with inequalities on different fields to retrieve docs. // Objective: Select documents where dividendYield is between 8 and 20 and pvp is less than or equal to 1. ...

At what point does the event loop in node.js stop running?

Could you please enlighten me on the circumstances in which the event loop of node.js terminates? How does node.js determine that no more events will be triggered? For instance, how does it handle situations involving an HTTP client or a file reading app ...

Refresh Vue/Nuxt Components Fully

Understanding how this.$forceUpdate() functions, I am not simply looking to re-render the component. In Nuxt applications, page components have asyncData() as a lifecycle method that runs before created(). I utilize this method to retrieve initial data an ...

Advantages of using individual CSS files for components in React.js

As someone diving into the world of Web Development, I am currently honing my skills in React.js. I have grasped the concept of creating Components in React and applying styles to them. I'm curious, would separating CSS files for each React Component ...

Using JSON to pass a function with parameters

I have a small issue that I'm struggling to solve. My dilemma lies in sending a JSON with a function, along with parameters. Typically, it's easy to send a function in JSON like this: var jsonVariable = { var1 : 'value1', var2 : &apos ...

Generate a Report in Embedded Mode using the PowerBI API

Attempting to generate a new report using data from an embedded view, but encountering continuous "This content isn't available" messages and receiving a 403 error from the reportEmbed.min.js during rendering. While able to create and save reports suc ...