Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KazChe/2988372651ce2ce786adb862a29ee5c0 to your computer and use it in GitHub Desktop.
Save KazChe/2988372651ce2ce786adb862a29ee5c0 to your computer and use it in GitHub Desktop.
const moment = require('moment');
var startDate = moment('2019-03-12');
var endDate = moment('2019-05-04');
var result = [];
var endOfMonth;
if (endDate.isBefore(startDate)) {
throw "End date must be greated than start date."
}
while (startDate.isBefore(endDate)) {
endOfMonth = startDate.clone().endOf('month').format("YYYY-MM-DD")
if(moment(endOfMonth).isAfter(endDate)) {
result.push(startDate.format("YYYY-MM-DD"),moment(endDate).format("YYYY-MM-DD"));
break;
} else {
result.push(startDate.format("YYYY-MM-DD"),endOfMonth);
startDate = moment(endOfMonth).add(1, 'day');
}
}
console.log('monthly',result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment