Get the days in a Week in Javascript, Week starts on Sunday

前端 未结 4 1010
长情又很酷
长情又很酷 2021-01-19 18:29

Here\'s the given input from the user:

Year = 2011, Month = 3 (March), Week = 2

I want to get the days in week 2 of March 2011 in JavaScript.

e.g. Su

相关标签:
4条回答
  • 2021-01-19 18:48

    Assuming that the first week of the month is the one with the first of the month in it, then the following function will return the date of the first day of the week given year, month and week number:

    /* 
       Input year, month and week number from 1
       Returns first day in week (Sunday) where
       first week is the first with 1st of month in it
    */
    function getFirstDayOfWeek(y, m, w) {
      var d = new Date(y, --m);
      d.setDate(--w * 7 - d.getDay() + 1);
      return d;
    }
    
    alert(getFirstDayOfWeek(2011,3, 2)); // Sun Mar 06 2011
    

    To get the rest of the days, just loop 6 times adding one to the date each time, e.g.

    function getWeekDates(y, m, w) {
      var d = getFirstDayOfWeek(y, m, w)
      var week = [new Date(d)];
      var i = 6;
      while (i--) {
        week.push(new Date(d.setDate(d.getDate() + 1)));
      }
      return week;
    }
    
    // Show week of dates
    var week = getWeekDates(2011,3, 2);
    for (var i=0, iLen=week.length; i<iLen; i++) {
      alert(week[i]);
    }
    
    0 讨论(0)
  • 2021-01-19 18:51

    A little modification of the first answer that worked for me:

    year = 2014;
    week = 31;//week number is 31 for the example, could be 120.. it will just jump trough years
    // get the date for the first day of the year               
    var firstDateOfYear = new Date(year,0,1);
    // set the date to the number of days for the number of weeks
    firstDateOfYear.setDate(firstDateOfYear.getDate()+(7 * (week-1))); 
    // get the number of the day in the week 0 (Sun) to 6 (Sat)
    var counter = firstDateOfYear.getDay();
    
    //make sunday the first day of the week
    for(i=0;i<counter;i++){
        firstDateOfYear.setDate(firstDateOfYear.getDate()-1)
    }
    
    var firstDateOfWeek = new Date(firstDateOfYear);    // copy firstDateOfYear
    
    var dateNumbersOfMonthOnWeek = [];                   // output array of date #s
    var datesOfMonthOnWeek = [];                         // output array of Dates
    
    for (var i = 0; i < 7; i++) {                        // for seven days...
        dateNumbersOfMonthOnWeek.push(                   // push the date number on
            firstDateOfWeek.getDate());                  // the end of the array
    
     datesOfMonthOnWeek.push(                         // push the date object on
         new Date(+firstDateOfWeek));                 // the end of the array
    
     firstDateOfWeek.setDate(
        firstDateOfWeek.getDate() + 1);              // move to the next day
    }
    
    0 讨论(0)
  • 2021-01-19 19:06

    I normally use Datejs for date manipulations in js. I'd do it like this:

    var firstDayOfWeek = new Date(year,month-1,1).moveToDayOfWeek(0,-1).addWeeks(week-1);
    var lastDayOfWeek = firstDayOfWeek.addDays(6);
    
    0 讨论(0)
  • 2021-01-19 19:14

    Given

    var year = 2011;
    var month = 3;
    var week = 2;
    

    and

    var firstDateOfMonth = new Date(year, month - 1, 1); // Date: year-month-01
    
    var firstDayOfMonth = firstDateOfMonth.getDay();     // 0 (Sun) to 6 (Sat)
    
    var firstDateOfWeek = new Date(firstDateOfMonth);    // copy firstDateOfMonth
    
    firstDateOfWeek.setDate(                             // move the Date object
        firstDateOfWeek.getDate() +                      // forward by the number of
        (firstDayOfMonth ? 7 - firstDayOfMonth : 0)      // days needed to go to
    );                                                   // Sunday, if necessary
    
    firstDateOfWeek.setDate(                             // move the Date object
        firstDateOfWeek.getDate() +                      // forward by the number of
        7 * (week - 1)                                   // weeks required (week - 1)
    );
    
    var dateNumbersOfMonthOnWeek = [];                   // output array of date #s
    var datesOfMonthOnWeek = [];                         // output array of Dates
    
    for (var i = 0; i < 7; i++) {                        // for seven days...
    
        dateNumbersOfMonthOnWeek.push(                   // push the date number on
            firstDateOfWeek.getDate());                  // the end of the array
    
        datesOfMonthOnWeek.push(                         // push the date object on
            new Date(+firstDateOfWeek));                 // the end of the array
    
        firstDateOfWeek.setDate(
            firstDateOfWeek.getDate() + 1);              // move to the next day
    
    }
    

    then

    • dateNumbersOfMonthOnWeek will have the date numbers of that week.
    • datesOfMonthOnWeek will have the date objects of that week.

    While this may seem like it is overkill for the job, much of this is required to make it work in all situations, like when the date numbers cross over to another month. I'm sure it could be optimised to be less verbose, though.

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