Skip to content

Instantly share code, notes, and snippets.

@KazChe
Created May 4, 2019 21:04
Show Gist options
  • Save KazChe/30f85efdf74835a65b14cc8c5cc8a02d to your computer and use it in GitHub Desktop.
Save KazChe/30f85efdf74835a65b14cc8c5cc8a02d to your computer and use it in GitHub Desktop.
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