Javascript - get array of dates between 2 dates

后端 未结 25 1139
傲寒
傲寒 2020-11-22 15:16
var range = getDates(new Date(), new Date().addDays(7));

I\'d like \"range\" to be an array of date objects, one for each day between the two dates

25条回答
  •  情歌与酒
    2020-11-22 15:47

    1. Generate an array of years:

      const DAYS = () => {
       const days = []
       const dateStart = moment()
       const dateEnd = moment().add(30, ‘days')
       while (dateEnd.diff(dateStart, ‘days') >= 0) {
        days.push(dateStart.format(‘D'))
        dateStart.add(1, ‘days')
       }
       return days
      }
      console.log(DAYS())
      
    2. Generate an arrays for month:

              const MONTHS = () => {
               const months = []
               const dateStart = moment()
               const dateEnd = moment().add(12, ‘month')
               while (dateEnd.diff(dateStart, ‘months') >= 0) {
                months.push(dateStart.format(‘M'))
                dateStart.add(1, ‘month')
               }
               return months
              }
              console.log(MONTHS())
      
    3. Generate an arrays for days:

              const DAYS = () => {
               const days = []
               const dateStart = moment()
               const dateEnd = moment().add(30, ‘days')
               while (dateEnd.diff(dateStart, ‘days') >= 0) {
                days.push(dateStart.format(‘D'))
                dateStart.add(1, ‘days')
               }
               return days
              }
              console.log(DAYS())
      

提交回复
热议问题