Search code examples
javascriptdatejs

Datejs get a string of days in a week


I use datejs, and I have two dates: startDate and endDate.

The dates are two objects of type date. I need to write a function that enumerates the days between these two dates.

the marge between is 7 days

Example:

startDate = 2012-10-30
endDate = 2012-11-05

I need to get a string of output like this:

30,31,01,02,03,04,05


Solution

  • var getDays = function (start, end) {
        var days = [],
            temp = start.clone();
    
        while (temp <= end) {
            days.push(temp.toString('dd'));
            temp.add(1).day();
        }
    
        return days.join(',');
    }
    
    var days = getDays(Date.parse('2012-10-30'), Date.parse('2012-11-05'));
    

    Returns a string such as "30,31,01,02,03,04,05".

    Hope this helps.