Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Given two dates generate weekly output dates
const moment = require('moment');
let startDate = moment('2019-03-12');
let endDate = moment('2019-05-04');
let result = [];
if (endDate.isBefore(startDate)) {
throw "End date must be greated than start date."
}
while (startDate.isBefore(endDate)) {
let endOfWeek = startDate.clone().endOf('week').format("YYYY-MM-DD")
if(moment(endOfWeek).isAfter(endDate)) {
result.push(startDate.format("YYYY-MM-DD"),moment(endDate).format("YYYY-MM-DD"));
break;
} else {
result.push(startDate.format("YYYY-MM-DD"),endOfWeek);
startDate = moment(endOfWeek).add(1, 'day');
}
}
console.log('weekly',result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.