Looking to fetch the date within the current week using AngularJS between Monday and Sunday?

How can I retrieve the dates for this week from Monday to Sunday?

For example, the dates for this week are: Monday = 23/11/2015 to Sunday 29/11/2015. I am looking to obtain the dates for every week, spanning from Monday to Sunday.

Technologies: AngularJS, JavaScript

Answer №1

In plain terms, you have the ability to specify the day of the week on a Date object in vanilla JavaScript with the following function:

function setDay(d, day, week_starts_monday) {
    var c_day = d.getDay();
    if (week_starts_monday && c_day === 0)
        c_day = 7; // adjust so `0` represents last Sunday, and `7` signifies this Sunday
    d.setDate(d.getDate() - c_day + day);
    return d;
}

For example, setting a date to represent Monday of the current week given that today is Friday the 27th would look like this:

var d = new Date;
setDay(d, 1); // Mon Nov 23 2015 18:51:45 GMT+0000 (GMT Standard Time)

As a reference point, 7 corresponds to the upcoming Sunday, while 0 signifies the previous Sunday or the present day if it happens to be a Sunday.

setDay(d, 7); // Sun Nov 29 2015 18:51:45 GMT+0000 (GMT Standard Time)

You can now loop through different days using a for loop as per your requirements,

var i, d = new Date;
for (i = 1; i <= 7; ++i) {
    setDay(d, i);
    // perform actions with `d`
}

A couple of things to keep in mind:

  • When transitioning between timezones, there may be unexpected outcomes. Consider executing this logic in UTC for reliability.
  • To go back from Sunday, utilize negative numbers accordingly.

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

Can trusted events in Chrome trigger a timeout with Synchronous Ajax requests?

Situation We are facing a situation where we need to open a new tab in browsers after an XHR / Ajax request is made by clicking on something. To achieve this, we set the Ajax request to be synchronous in order to maintain the context of the trusted click ...

Electron Applies a 1px Border Surrounding the titleBarOverlay

I am completely new to Electron and Node.js, so please forgive me if I misuse any terminology. Currently, I am developing a Windows 10 application and I would like to customize the title bar area of my app similar to other Electron applications such as Gi ...

Submitted input in a text field is automatically displayed in a separate text box

Below is a snippet of HTML code from my AngularJS application <input type="text" class="input-xlarge" id="user_name" ng-model="myModel.text" name="user_name" rel="popover" data-content="Enter your first and last name." data-original-titl ...

Is it possible to retrieve the key value of the array element that triggered a function call?

I'm looking to streamline my webpage's code by reading an XML file that contains a variable number of objects. In my JavaScript code, I create an array to store each object and loop through the XML data to generate them. As I iterate through the ...

Expanding a feature that modifies the CSS properties of every input element

Creating a text tracking (letter-spacing) demonstration where range sliders are used to update CSS properties. I'm looking to improve the functionality so that the labels also update dynamically, without repeating output2.textContent = this.value;. An ...

Navigating with Vue Router on Internet Information Services (IIS)

I am encountering difficulties understanding why my routes are failing when I refresh my VueJS application hosted on IIS. Everything works fine during normal browsing, but once I press F5 or update view information through a button, a 403 error is thrown. ...

A step-by-step guide on setting up flow types for @material-ui/core version 4

Is there a way to install flow types for material-ui/core version 4.x.x? It seems like the last update was for version 1.x.x here. The documentation on this topic is quite limited here. I'm unsure if there is still support for this, especially since t ...

Challenges with creating a responsive YouTube iframe using the API

Looking to integrate a Youtube video using the iFrame API to capture events effectively, embedding the player alone is not an option. Following the documentation, everything functions correctly with code like: var tag = document.createElement('scrip ...

Is there a way to include a variable in the URL for an AJAX call?

When working with a PHP function that requires an ID and adding a variable to the Ajax URL, the following code snippet can be helpful: PHP Code: function get_json_selected($purpose) { $ids = explode(",", $this->input->post("ids")); ...

Restricting href Usage Based on Session

I find myself in a challenging situation without a clear solution at hand. Within this code, I am utilizing a link and a button for which I need to save the page in a database. Therefore, creating a server control is not an option as it will not be render ...

Is there a way to position the menu above the button?

I need some help with my menu. Currently, it is showing up below the button and within my footer instead of above the content like I want it to. If anyone can assist me in understanding what I am doing wrong, I would greatly appreciate it. Thank you for ta ...

JavaScript: Efficiently Replacing Multiple Text Instances

To ensure that users do not input multiple empty paragraphs in the text editor, I have found a method to eliminate a single <p><br></p> from the text within the editor message. var str = content.replace('<p><br></p> ...

How can an AngularJs developer effectively transition to learning ReactJS from Facebook?

As an experienced AngularJS developer, I am now looking to delve into the world of ReactJS from the ground up. I'm seeking guidance on what my learning curve should look like as I dive into ReactJS. Any help would be greatly appreciated! I'm ...

Is there a way to retrieve an attribute from a nested object in a loop while using VueJS?

Currently, I am facing an issue while trying to access a property within a nested object using a loop in my vuejs project. It's strange because similar nested objects work fine except for one. <template> <div> <tr v-for=" ...

The error message "ReferenceError: process is not defined" is caused by a module located at "../../node_modules/graphql/jsutils/instanceOf

While working on my latest project, an Instagram clone, I made use of GraphQL subscriptions to implement the functionality for liking/unliking posts. For my client, I utilized npx create-react-app to set up the front end and express for the server. Within ...

Could the issue at hand possibly stem from the fact that the router isn't fully operational? It appears that router.query

Having trouble retrieving the parameters from the URL using router.query. I've tried various approaches but keep getting an undefined result. It seems like I'm on the right track, though. Highlighted query param in yellow... https://i.stack.img ...

Organize a collection containing various spans using jQuery

Having trouble sorting a list with spans inside. For example: <a href="">Sort by name</a> <a href="">Sort by year</a> <a href="">Sort by fruit</a> <ul> <li> <span class="name">Carl</span> & ...

Avoid the div from covering the scrollbar within its containing parent div

Background: Trying to resolve an issue mentioned here: https://github.com/likeastore/ngDialog/issues/94 Challenge: If you open this plnkr link: http://plnkr.co/edit/qKJiNwyivqJVCAtyhwYR?p=preview and attempt to hold and drag the scrollbar with the mouse, ...

Top method for identifying genuine users and preventing bots

Utilizing a Maps API can be costly, especially with the fees per request To minimize requests, I heavily rely on caching techniques The API is invoked on every pageload, but unnecessary for non-human users like googlebot What would be the most effective ...

Calculating the angle between two planes in Three.js is a useful technique

I'm currently developing a project with the three.js library, and I am curious about how to calculate the angle (let's refer to it as α) between two plane geometries. You can find a helpful visual representation of the planes in this image. ...