以下是来自用户的给定输入:
年= 2011年,月= 3(3月),周= 2
我希望在2011年3月第2周的JavaScript中获得这些日子。
e.g。周日6日,周一7日,周二8日,周三9日,周四10日,周五11日,周六12日
有什么想法吗?谢谢!
答案 0 :(得分:8)
鉴于
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
将包含该周的日期对象。虽然这看起来似乎对工作来说太过分了,但要使其在所有情况下都能正常运行,例如日期数字跨越到另一个月时需要这样做。我确信它可以被优化为不那么冗长。
答案 1 :(得分:0)
我通常在js中使用Datejs进行日期操作。我会这样做:
var firstDayOfWeek = new Date(year,month-1,1).moveToDayOfWeek(0,-1).addWeeks(week-1);
var lastDayOfWeek = firstDayOfWeek.addDays(6);
答案 2 :(得分:0)
假设该月的第一周是该月的第一周,那么以下函数将返回给定年,月和周数的第一天的日期:
/*
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次,每次添加一个日期,例如
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]);
}
答案 3 :(得分:0)
对我有用的第一个答案的一点修改:
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
}