Reputation: 426
I'm using Moment.js to calculate the different between 2 given dates. Specially my task is to calculate weekdays of the given date range and not calculate and weekend and also special given date like holidays.
I found this nice library to do the calculation easily which is integrated with Moment.js : Weekday Calc with Moment Library
This is the code I used to get specific dates between a given date range.
moment().weekdayCalc({
rangeStart: '1 Apr 2015',
rangeEnd: '31 Mar 2016',
weekdays: [1,2,3,4,5],
exclusions: ['6 Apr 2015','7 Apr 2015'],
inclusions: ['10 Apr 2015']
})
From this code, I get only a numeric output. ( Result is 260 )
Can I get a dates list in a JavaScript array like this :
["yyyy-mm-dd" , "yyyy-mm-dd" , "yyyy-mm-dd" , "yyyy-mm-dd" , ..... ]
Upvotes: 0
Views: 598
Reputation: 1947
You can easily achieve what you want with date-fns which I'd also encourage you to use instead of Moment for other reasons. I haven't tested the code below but I'm pretty sure it would be something along these lines.
const { eachDay, isWeekend, format } = require('date-fns');
var daysArray = eachDay(
new Date(2014, 9, 6),
new Date(2014, 9, 10)
);
var weekdaysArray = daysArray.filter(day => !isWeekend(day));
var formattedWeekdays = weekdaysArray.map(day => format(day, 'yyyy-mm-dd'));
Upvotes: 1