Given a start and end date, create an array of the dates between the two

后端 未结 7 2411
难免孤独
难免孤独 2021-01-04 12:39

Right now, I have this on my page:



        
7条回答
  •  半阙折子戏
    2021-01-04 13:26

    I have read the given answer and this is what I came out with. Note that I started from the answer of @pimvdb

    // return an array composed of a list of the days' number 
    // from 1 month ago until today, not included
    function getAllDays() {
     var e = moment();
     var s = moment().subtract('months', 1);
     var a = []
      // While the updated start date is older, perform the loop.
      while(s.isBefore(e)) {
       // Update the format according to moment js documentations format().
       a.push(s.format("MMM - DD"));
       s = s.add('days', 1);
      }
     return a;
    }
    

    Simply call the function anywhere in your code:

    getAllDays();
    

    see also: MomentJs library

提交回复
热议问题