Skip to content

Instantly share code, notes, and snippets.

@ilhamarrouf
Created April 1, 2021 07:10
Show Gist options
  • Save ilhamarrouf/ab1dc9046b3de453391f1dcd272e156c to your computer and use it in GitHub Desktop.
Save ilhamarrouf/ab1dc9046b3de453391f1dcd272e156c to your computer and use it in GitHub Desktop.
Iterate Date Range
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