Mr B
Mr B

Reputation: 4130

Get the dates of all the Mondays between two dates in JavaScript

I'm trying to return the dates of all the Mondays between two dates. Here is what I've done so far.

function populate_week_range_options(){
    var start_week_date = new Date(2012, 7-1, 2); // no queries exist before this
    //console.log(start_week_date);
    var todays_date = new Date();

    // array to hold week commencing dates
    var week_commencing_dates = new Array();
    week_commencing_dates.push(start_week_date);

    while(start_week_date < todays_date){
        var next_date = start_week_date.setDate(start_week_date.getDate() + 1);

        start_week_date = new Date(next_date);
        //console.log(start_week_date);
        if(start_week_date.getDay() == 1){
            week_commencing_dates.push(start_week_date);
        }

       //
    }

    return week_commencing_dates;
}
console.log(populate_week_range_options());

Based on the documentation I've read on the getDay() function, it returns an index representing a day of the week (0 to 6, Sunday to Saturday respectively)

However, for some reason my function returns Tuesdays instead - I can't figure out why!

I changed the if statement to compare the day index against 0, and this returns the dates I'm expecting but I'm guessing this the incorrect way to do this as 0 is for Sunday.

Any help is appreciated :-)

Upvotes: 1

Views: 3560

Answers (3)

Mr B
Mr B

Reputation: 4130

Seems like I wasn't getting the dates I expected because of the way I was pushing elements to the array. I changed my code so that it creates a new variable for every element that is to be pushed to the array. I don't code in JavaScript so don't fully understand this, appreciate it if anyone can explain this behavior. Nonetheless, here is my updated code returning the expected dates:

function populate_week_range_options(){
    var start_week_date = new Date(2012, 7-1, 2); // no queries exist before this
    var todays_date = new Date();

    // array to hold week commencing dates
    var week_commencing_dates = new Array();
    var first_monday_date = new Date(2012, 7-1, 2); // no queries exist before this
    week_commencing_dates.push(first_monday_date);

    while(start_week_date < todays_date){
        var next_date = start_week_date.setDate(start_week_date.getDate() + 1);

        var next_days_date = new Date(next_date);
        day_index = next_days_date.getDay();
        if(day_index == 1){
            week_commencing_dates.push(next_days_date);
        }
        // increment the date
        start_week_date = new Date(next_date);
    }

    return week_commencing_dates;
}

Upvotes: 2

Frank W
Frank W

Reputation: 891

It's really odd, I've tested your code and the funny thing is, it shows me Monday in console.log() when I look inside the if-Block and inside the while-loop (on the LAST added element in the array), but when I look at the end into the full array (so when some milliseconds have passed) all dates are Tuesday. So the day changes after ~1 millisecond. Sorry got no answer for you, but wanted to mention this so anyone else might figure it out.

Upvotes: 1

Sumurai8
Sumurai8

Reputation: 20737

getDay() returns the day according to the local timezone. I am assuming that your date is converted from an UTC time to a local time with the offset set up in your computer, which will change the date, and therefore the day of the week. I can't really test this here though.

Edit: I think you should be using getUTCDay() here.

Upvotes: 1

Related Questions