How to enumerate dates between two dates in Moment

后端 未结 7 1507
太阳男子
太阳男子 2020-12-13 03:58

I have two moment dates:

var fromDate = moment(new Date(\'1/1/2014\'));
var toDate   = moment(new Date(\'6/1/2014\'));

Does moment provide

相关标签:
7条回答
  • 2020-12-13 04:10

    Got it for you:

    var enumerateDaysBetweenDates = function(startDate, endDate) {
        var now = startDate.clone(), dates = [];
    
        while (now.isSameOrBefore(endDate)) {
            dates.push(now.format('M/D/YYYY'));
            now.add(1, 'days');
        }
        return dates;
    };
    

    Referencing now rather than startDate made all the difference.

    If you're not after an inclusive search then change .isSameOrBefore to .isBefore

    Fiddle: http://jsfiddle.net/KyleMuir/sRE76/118/

    0 讨论(0)
  • 2020-12-13 04:10

    Using moment library and for loop you can enumerate between two dates.

    let startDate = moment('2020-06-21');
    let endDate = moment('2020-07-15');
    let date = [];
    
    for (var m = moment(startDate); m.isBefore(endDate); m.add(1, 'days')) {
        date.push(m.format('YYYY-MM-DD'));
    }
    
    console.log(date)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

    0 讨论(0)
  • 2020-12-13 04:11

    use moment and work with while loop, code will run in loop untill startDate is equal to endDate and push startDate and then increment it with 1 day so can get next date

    function enumerateDaysBetweenDates (startDate, endDate){
      let date = []
      while(moment(startDate) <= moment(endDate)){
        date.push(startDate);
        startDate = moment(startDate).add(1, 'days').format("YYYY-MM-DD");
      }
      return date;
    }
    

    you can test it by calling function like this

    let dateArr = enumerateDaysBetweenDates('2019-01-01', '2019-01-10');
    
    0 讨论(0)
  • 2020-12-13 04:19

    Momentjs doesn't provide this by itself but there is a plugin which offers it: moment-range.

    Specifically, check out the Iteration docs.

    0 讨论(0)
  • 2020-12-13 04:19

    You can easily enumerate with moment.js Here is a more generic solution for days, weeks, months or years:

    https://gist.github.com/gvko/76f0d7b4b61b18fabfe9c0cc24fc3d2a

    0 讨论(0)
  • 2020-12-13 04:20

    As an extension of Kyle's answer - I've been trying to get this to work with Unix timestamps and after lots of trial and error I got it to work and thought I'd post it here in case anyone is seeking the same thing and needs it. See my code below:

    fromDate = moment.unix(req.params.dateFrom).format('YYYY-MM-DD')
    toDate = moment.unix(req.params.dateTo).format('YYYY-MM-DD')
    
    // Returns an array of dates between the two dates
    function enumerateDaysBetweenDates(startDate, endDate) {
        startDate = moment(startDate);
        endDate = moment(endDate);
    
        var now = startDate, dates = [];
    
        while (now.isBefore(endDate) || now.isSame(endDate)) {
            dates.push(now.format('YYYY-MM-DD'));
            now.add(1, 'days');
        }
        return dates;
    };
    

    Note that I convert it to Unix, then convert that value to moment again. This was the issue that I had, you need to make it a moment value again in order for this to work.

    Example usage:

    fromDate = '2017/03/11' // AFTER conversion from Unix
    toDate = '2017/03/13' // AFTER conversion from Unix
    
    console.log(enumerateDaysBetweenDates(fromDate, toDate));
    

    Will return:

    ['2017/03/11', '2017/03/12', '2017/03/13']
    
    0 讨论(0)
提交回复
热议问题