Angular and JavaScript: Today is Monday and the date is exactly one week old

I am currently working on an Angular application that is connected to a REST API. In order to minimize the number of requests made, I have implemented a method to store all data in the local storage.

.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
        var object = {value: value, timestamp: new Date().getTime()}
        $window.localStorage[key] = JSON.stringify(object);
    },
    setObject: function(key, value) {
        var object = {value: value, timestamp: new Date().getTime()}
        $window.localStorage[key] = JSON.stringify(object);
    },
  }
}]);

As new data is expected every Monday, my goal is to determine if the data stored in the local storage is older than 7 days and if the current day of the week is Monday.

If these conditions are met, then the data should be refreshed. I am seeking for suggestions on how to tackle this simple problem. Any ideas?

Answer №1

Alright, I've got the solution ready. This should do the trick!

function findNextDayOfWeek(date, dayOfWeek) {
        // Make sure to validate date and dayOfWeek before proceeding ;)
        var newDate = new Date(date.getTime());
        if (date.getDay() === 1 && dayOfWeek === 1) {
          newDate.setDate(date.getDate() + 7)
        }
        else {
          newDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
        }
        return newDate;
    }

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

Encountering a "403 Forbidden" error upon deployment to the server due to

Situation: I am developing a website using Spring web MVC, JSP, and HTML/JavaScript. One of the features I have implemented is a search function that communicates with imdbapi.org to retrieve movie/TV show information in JSON format via AJAX. The JSON resp ...

Using ReactJS and Ruby on Rails to implement an AJAX delete request

There is a component named Items residing inside a parent component called ItemsContainer. A click on a button within the Items component triggers an Ajax function to delete that specific Item. However, I am encountering a 500 error message at the moment ...

Choose an option from a dropdown menu using JavaScript

Currently, I am working on a project that involves using javascrypt in conjunction with selenium. While attempting to search for and select an item from a list, I have encountered difficulties in selecting the element even after successfully locating it. I ...

ReactJS encounters errors even when the backend is returning a 200 OK status code

My ReactJS frontend receives an error when sending a GET request, even though the django backend terminal shows [10/Mar/2018 23:31:08] "GET /post/ HTTP/1.1" 200 930. I am using redux sagas in my project. In src/index.js: (index.js code snippet here) In ...

Arranging CouchDB/Couchbase view based on the quantity of keys

Looking to create a view that displays the top 10 tags used in my system. While it's simple to get the count using _count in the reduce function, the list is not sorted by the numbers. Is there a way to accomplish this? function(doc, meta) { if(doc ...

Unable to access the suggestion list within the modal

I incorporate the PrimeNG AutoComplete feature within a PrimeNG Dialog, displaying a list of elements below the AutoComplete in the dialog. My objectives are: To set the max-height of the modal dialog to 300, and have a visible scrollbar if the total ...

I am unable to store rich text fields using LocalStorage

My goal is to store form data in localStorage, but I'm facing an issue with rich text fields not saving their data. Regular HTML fields like textboxes work fine. In my Razor markup for the field (using MVC), here is an example: <div class="form-g ...

Embracing Error Handling with ES6 Promises

I am seeking clarification on how errors travel through a series of then continuations to a catch continuation. Consider the following code: Promise.reject(new Error("some error")) .then(v => v + 5) .then(v => v + 15) .catch(er ...

Ajax request returns a 500 error, yet the request successfully executes

I have set up an API controller to manage an ajax request. Each time the Ajax request is sent from the script below, I encounter a 500 error: POST http://localhost:58463/api/Reservations 500 (Internal Server Error) jquery-2.1.0.min.js:4 l.cors.a.crossDomai ...

Angularjs: Troubleshooting issues with ng-click functionality within a directive

I have encountered an issue while building a tree-view where each branch is generated within a directive. The problem I am facing is that the ng-click event does not seem to be working as expected. The ng-click event is included at the end of the variable ...

Resize an image to fit perfectly within a material-ui grid

Is there a way to resize images within a grid layout without them getting cropped? Each image I try to fit into the top horizontal grid behaves differently due to varying dimensions. Instead of stretching and fitting into the container, the images end up b ...

Tips for detecting successful file downloads from the client side using Mean/AngularJS

I have developed a chat application with the capability to send files through chat windows. I am now looking to automatically delete files from the server once they have been successfully downloaded by clients. My technology stack includes MEAN. rou ...

Issue with Vue-Validator form validation not functioning properly on JS Fiddle

I'm having trouble with vue-validator on JSFiddle. Can someone please assist in troubleshooting the issue so I can proceed with my main question? Check out JSFiddle Html: <div id="app"> <validator name="instanceForm"> & ...

Automatically identifying cloud functions ready for deployment with the gcloud command line interface

When utilizing Firebase tools, the deployment process is streamlined with auto-detection of available functions by issuing the command: firebase deploy --only functions However, for more extensive control over environment variables and VPC connectors, we ...

Importing vs Referencing Videos in Next.js - What is the Best Practice for Video Integration in Next.js?

I'm looking to use a video as a background in my banner design. Typically, with images in Next.js, I would import them and then pass the import as src (for example: import img from '@images/about-us-page/img.svg'). However, when attempting ...

I am having trouble with my Vue nested For loop as it is only returning the first value of the second array. What could be

I am currently utilizing a nested For loop to retrieve data from JSON and then returning a variable for Vue frontend access. Oddly enough, I am only able to retrieve values from the initial element of the nested array. Can anyone assist with this issue? It ...

Material UI React Autocomplete Component

I'm currently working on integrating an Autocomplete component using the Material UI library. However, I've encountered a challenge - I'm unsure of how to properly pass the value and onChange functions, especially since I have a custom Text ...

Is it possible to hide a fixed header on scroll in a mobile device?

I am facing an issue with my Wordpress site where I want the header to hide when the user scrolls. Despite trying various codes, I have been unable to make it work. The reason for wanting to hide the header is that on mobile devices, it causes scroll prob ...

Sending a file through an Ajax POST request to a PHP server

I am attempting to upload the HTML input file to my PHP file using a different method than the traditional synchronous HTML forms. The problem I am encountering is that it seems like I am not correctly POST'ing the input file to my PHP file because t ...

Testing API calls with ReactJS

I'm diving into Reactjs development and have encountered a challenge. I created an App that makes API calls to https://jsonplaceholder.typicode.com/users. However, when testing the API call, I ran into issues. In addition, I built a simple WebApi pro ...