Rotan075
Rotan075

Reputation: 2615

Get all dates between two dates and return only the first of the month

I have some code which returns all the dates between two predefined dates. This is very nice but I was wondering how I could only return the values that corresponds to the first of the month.

So that I get the following desired result:

Mon Feb 01 2016 01:00:00 GMT+0100 (W. Europe Standard Time)
Tue Mar 01 2016 01:00:00 GMT+0100 (W. Europe Standard Time)
Fri Apr 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Sun May 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Wed Jun 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Fri Jul 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Mon Aug 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Thu Sep 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Tue Nov 01 2016 01:00:00 GMT+0100 (W. Europe Standard Time)

My JS code:

$('#getBetween').on('click', function () {
    var start = new Date("2016-01-01"),
        end = new Date("2016-12-01"),
        currentDate = new Date(start),
        between = []
    ;

    while (currentDate <= end) {
        between.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 1);
    }

    $('#results').html(between.join('<br> '));
});

DEMO HERE

What kind of method do I need to create which enables me to allocate the first of the month.

Upvotes: 0

Views: 1518

Answers (2)

KarelG
KarelG

Reputation: 5244

You can simply construct a new Date object while adding a month to it. Here is a snippet of it:

currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);

So the currentDate picks the year of the previous value, adds a month to the previous value and sets the day to 1 (to ensure that you have the first day) when constructing a new Date object. By using this manner, you would prevent unnecessary loops (like from day 2 -> 31 in the month January )

$('#getBetween').on('click', function () {
    var start = new Date("2016-01-01"),
        end = new Date("2016-12-01"),
        currentDate = new Date(start),
        between = []
    ;

    while (currentDate <= end) {
        between.push(new Date(currentDate));
        currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
    }
    
    $('#results').html(between.join('<br> '));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="getBetween">Get Between Dates</button>
<div id="results"></div>

This works also if the end date is in different year.

$('#getBetween').on('click', function () {
    var start = new Date("2016-01-01"),
        end = new Date("2017-06-01"), // end date is now mid 2017
        currentDate = new Date(start),
        between = []
    ;

    while (currentDate <= end) {
        between.push(new Date(currentDate));
        currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
    }
    
    $('#results').html(between.join('<br> '));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="getBetween">Get Between Dates</button>
<div id="results"></div>

Upvotes: 1

Jeremy
Jeremy

Reputation: 279

Just replace in your while loop :

currentDate.setDate(currentDate.getDate() + 1);

per :

currentDate.setMonth(currentDate.getMonth() + 1);

Upvotes: 1

Related Questions