繁体   English   中英

在 Javascript 中获取一周中的天数,一周从星期日开始

[英]Get the days in a Week in Javascript, Week starts on Sunday

这是来自用户的给定输入:

年 = 2011,月 = 3(三月),周 = 2

我想在 JavaScript 中获得 2011 年 3 月第 2 周的日子。

例如 6 日星期日、7 日星期一、8 日星期二、9 日星期三、10 日星期四、11 日星期五、12 日星期六

有任何想法吗? 谢谢!

给定

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

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

}

然后

  • dateNumbersOfMonthOnWeek将具有该周的日期编号。
  • datesOfMonthOnWeek将具有该周的日期对象。

虽然这对于这项工作来说似乎是多余的,但其中大部分是使其在所有情况下都能正常工作的必要条件,比如当日期数字跨越到另一个月份时。 不过,我确信它可以优化为不那么冗长。

对对我有用的第一个答案进行了一些修改:

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
}

我通常使用Datejs在 js 中进行日期操作。 我会这样做:

var firstDayOfWeek = new Date(year,month-1,1).moveToDayOfWeek(0,-1).addWeeks(week-1);
var lastDayOfWeek = firstDayOfWeek.addDays(6);

假设该月的第一周是该月的第一周,那么以下 function 将返回给定年、月和周数的一周的第一天的日期:

/* 
   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

要获得当天的 rest,只需循环 6 次,每次将日期加一,例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM