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

前端 未结 6 2197
渐次进展
渐次进展 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)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

    0 讨论(0)
  • 2020-12-06 20:09

    Here is my solution using Moment.js

    Next 7 days

    let days = [];
    let daysRequired = 7
    
    for (let i = 1; i <= daysRequired; i++) {
      days.push( moment().add(i, 'days').format('ffffdd, Do MMMM YYYY') )
    }
    
    console.log(days)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

    Just in case if you need previous 7 days

    let days = [];
    let daysRequired = 7
    
    for (let i = daysRequired; i >= 1; i--) {
      days.push( moment().subtract(i, 'days').format('ffffdd, Do MMMM YYYY') )
    }
    
    console.log(days)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

    0 讨论(0)
  • 2020-12-06 20:09

    You can set the variable dateString to whatever you want and in the loop you just increase the day. Then you will get the dates, but I think in a different format.

    var dateString = '22 Feb 2012';
    var actualDate = new Date(dateString);
    var newDate;
    
    for(var i=1; i<=7; i++){
     newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+i);
    }
    
    0 讨论(0)
  • 2020-12-06 20:22

    An initial date:

    var startingDay = new Date(year, month, day);
    

    A whole week from startingDay:

    var thisDay = new Date();
    for(var i=0; i<7; i++) {
      thisDay.setDate(startingDay.getDate() + i);
      console.log(thisDay.format());
    }
    

    The formatting function:

    Date.prototype.format = function(){
        var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];        
        var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        return days[this.getDay()]
              +", "
              +this.getDate()
              +" "
              +months[this.getMonth()] 
              +" "
              +this.getFullYear();
    };
    
    0 讨论(0)
  • 2020-12-06 20:22
    var feb22 = new Date(2012, 1, 22);
    var feb23 = new Date(feb22.getTime() + 1000*60*60*24);
    

    ...and so on

    0 讨论(0)
  • 2020-12-06 20:25

    This seems to be what you're looking for:

    function GetDates(startDate, daysToAdd) {
        var aryDates = [];
    
        for (var i = 0; i <= daysToAdd; i++) {
            var currentDate = new Date();
            currentDate.setDate(startDate.getDate() + i);
            aryDates.push(DayAsString(currentDate.getDay()) + ", " + currentDate.getDate() + " " + MonthAsString(currentDate.getMonth()) + " " + currentDate.getFullYear());
        }
    
        return aryDates;
    }
    
    function MonthAsString(monthIndex) {
        var d = new Date();
        var month = new Array();
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";
    
        return month[monthIndex];
    }
    
    function DayAsString(dayIndex) {
        var weekdays = new Array(7);
        weekdays[0] = "Sunday";
        weekdays[1] = "Monday";
        weekdays[2] = "Tuesday";
        weekdays[3] = "Wednesday";
        weekdays[4] = "Thursday";
        weekdays[5] = "Friday";
        weekdays[6] = "Saturday";
    
        return weekdays[dayIndex];
    }
    
    var startDate = new Date();
    var aryDates = GetDates(startDate, 7);
    console.log(aryDates);​
    ​
    

    Result (as of today):

    ["Thursday, 5 April 2012",
     "Friday, 6 April 2012", 
     "Saturday, 7 April 2012", 
     "Sunday, 8 April 2012", 
     "Monday, 9 April 2012", 
     "Tuesday, 10 April 2012", 
     "Wednesday, 11 April 2012", 
     "Thursday, 12 April 2012"]
    

    Here's a working fiddle.

    0 讨论(0)
提交回复
热议问题