使用下面的代碼,直到這個星期,對象是返回一個下拉式的「正確」的周,即36周爲37年9月5日爲9月12日,等奇日期行爲
由於這個月已經變成了十月,現在這些星期正在被錯誤地返回。從LEAP.Schedule對象
代碼:
/* --- LEAP Namespace --- */
var LEAP = {};
/* --- LEAP.Schedule Object --- */
LEAP.Schedule = function(){//init
this.weeks = [];
this.calculateWeeks();
};
LEAP.Schedule.prototype.calculateWeeks = function(){
this.date = new Date (2011, 8, 5); // First week of new school year
this.num = 36; // Calendar number of this week
this.weeks.push(new LEAP.Schedule.week(this.date, this.num));
for (var i = 1; i < 51; i++) {
var week = i * 7;
var updated_date = new Date();
updated_date.setDate(this.date.getDate() + week);
if (this.num > 51) {
this.num = 0;
}
this.num++;
this.weeks.push(new LEAP.Schedule.week(updated_date, this.num));
}
};
LEAP.Schedule.prototype.getWeeks = function(){
return this.weeks;
};
/* --- LEAP.Schedule.week Object --- */
LEAP.Schedule.week = function(n_date, n_week){
this.week = n_week;
this.date = n_date;
this.year = this.date.getFullYear();
this.month = this.date.getMonth();
this.month += 1;
this.day = this.date.getDate();
var mydate = new Date(this.date);
this.end_date = mydate.setDate(mydate.getDate() + 6);
};
LEAP.Schedule.week.prototype.getJSDate = function(){
return this.date;
};
LEAP.Schedule.week.prototype.getStartDate = function(){
return this.year + "-" + pad(this.month) + "-" + pad(this.day);
};
LEAP.Schedule.week.prototype.getEndDate = function(){
end_of_week = new Date(this.end_date);
var year = end_of_week.getFullYear();
var month = pad(end_of_week.getMonth() + 1);
var day = pad(end_of_week.getDate());
return year + "-" + month + "-" + day;
};
LEAP.Schedule.week.prototype.getLabel = function(){
return "Week " + this.week + ": " + this.day + (this.day==1||this.day==21||this.day==31?"st":this.day==2||this.day==22?"nd":this.day==3||this.day==23?"rd":"th") + " " + ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][this.month-1] + " " + this.year;
};
pad = function (n) {
return n>9 ? n : "0"+n;
};
代碼初始化/顯示該對象:
WeeklyUpdate.init = function() {
var Scheduleobject = new LEAP.Schedule();
var weeks = Scheduleobject.getWeeks();
var dispHTML = '<p>weeks.length: ' + weeks.length + '</p>';
for (var i = 0; i < weeks.length; i++) {
if (i % 2 > 0) {
dispHTML += '<div style="background:#ccc;">';
} else {
dispHTML += '<div style="background:#fff;">';
}
dispHTML += '<p>i: ' + i + '</p>';
dispHTML += '<p>getJSDate: ' + weeks[i].getJSDate() + '</p>';
dispHTML += '<p>getStartDate: ' + weeks[i].getStartDate() + '</p>';
dispHTML += '<p>getEndDate: ' + weeks[i].getEndDate() + '</p>';
dispHTML += '<p>getLabel: ' + weeks[i].getLabel() + '</p>';
dispHTML += '</div>';
}
$('div#wrapper').html(dispHTML);
//WeeklyUpdate.displayWeekFilter(weeks);
}
輸出(三週後修整):
weeks.length: 51
i: 0
getJSDate: Mon Sep 05 2011 00:00:00 GMT+0100 (GMT Daylight Time)
getStartDate: 2011-09-05
getEndDate: 2011-09-11
getLabel: Week 36: 5th Sep 2011
i: 1
getJSDate: Wed Oct 12 2011 13:58:02 GMT+0100 (GMT Daylight Time)
getStartDate: 2011-10-12
getEndDate: 2011-10-18
getLabel: Week 37: 12th Oct 2011
i: 2
getJSDate: Wed Oct 19 2011 13:58:02 GMT+0100 (GMT Daylight Time)
getStartDate: 2011-10-19
getEndDate: 2011-10-25
getLabel: Week 38: 19th Oct 2011
我已經看了這幾次,但我越來越困惑!有任何想法嗎?我相信這是非常明顯的。
乾杯
非常感謝Rob :) – dunc
不客氣。我建議的一週可以通過考慮[ISO 8601](http://en.wikipedia.org/wiki/ISO_8601#Week_dates)日期規範來改進:周計數器重置爲一次*僅噹噹周的星期四是新年的第一個星期四。 –