下面是用户给出的输入:
年= 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
将有该星期的日期对象。
虽然这看起来有点小题大做,但这是所有情况下都需要的,比如当日期数字交叉到另一个月时。我确信它可以被优化得不那么冗长。
我通常在js中使用Datejs进行日期操作。我想这样做:
var firstDayOfWeek = new Date(year,month-1,1).moveToDayOfWeek(0,-1).addWeeks(week-1);
var lastDayOfWeek = firstDayOfWeek.addDays(6);
假设该月的第一周是该月的第一天,那么下面的函数将返回给定年、月和周号的该周第一天的日期:
/*
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
要得到剩下的天数,只需循环6次,每次在日期上加1,例如
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]);
}
对第一个答案的一点修改:
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
}