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

∥☆過路亽.° 提交于 2019-12-04 03:55:53

问题


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. Sunday 6th, Monday 7th, Tuesday 8th, Wednesday 9th, Thursday 10th, Friday 11th, Saturday 12th

Any ideas? Thanks!


回答1:


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.




回答2:


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);



回答3:


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]);
}



回答4:


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
}


来源:https://stackoverflow.com/questions/6730246/get-the-days-in-a-week-in-javascript-week-starts-on-sunday

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!