How to get next seven days from X and format in JS

前端 未结 6 2237
渐次进展
渐次进展 2020-12-06 19:25

I want to print something like this (a 7-day calendar) but with the ability to start from any date I want.

Monday, 1 January 2011
Tuesday, 2 January 2011
Wed         


        
6条回答
  •  无人及你
    2020-12-06 20:02

    If you need next 7 weekdays starting from today

    const isWeekday = (date) => {
    	return date.weekday()!==0 && date.weekday()!==6
    }
    
    const weekdays=[];
    let numberOfDaysRequired = 7
    let addDaysBy = 1
    moment.locale('en')
    while(weekdays.length < numberOfDaysRequired)
    {
      const d = moment().add(addDaysBy, 'days')
      if(isWeekday(d))
      {
      	weekdays.push(d.format('ffffdd, Do MMMM YYYY'))
      	
      }
      addDaysBy++;
    }
    
    console.log(weekdays)

提交回复
热议问题