Created
April 1, 2021 07:10
-
-
Save ilhamarrouf/ab1dc9046b3de453391f1dcd272e156c to your computer and use it in GitHub Desktop.
Iterate Date Range
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 a = moment('2013-01-01'); | |
let b = moment('2013-06-01'); | |
// If you want an exclusive end date (half-open interval) | |
for (let m = moment(a); m.isBefore(b); m.add(1, 'days')) { | |
console.log(m.format('YYYY-MM-DD')); | |
} | |
// If you want an inclusive end date (fully-closed interval) | |
for (let m = moment(a); m.diff(b, 'days') <= 0; m.add(1, 'days')) { | |
console.log(m.format('YYYY-MM-DD')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment