Created
May 4, 2019 21:06
-
-
Save KazChe/2988372651ce2ce786adb862a29ee5c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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