Created
May 4, 2019 21:04
-
-
Save KazChe/30f85efdf74835a65b14cc8c5cc8a02d to your computer and use it in GitHub Desktop.
Given two dates generate weekly output dates
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'); | |
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