Odd date behaviour
Using the following code, up until this week, the object was returning a drop-down with the "correct" weeks, i.e. week 36 as 5th september, 37 as 12th september, etc.
Since the month has changed to October, the weeks are now being returned incorrectly.
Code from LEAP.Schedule object:
/* --- 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.Sc开发者_开发技巧hedule.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;
};
Code initialising/displaying this Object:
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);
}
Output (trimmed after three 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
I've looked through this a few times but I'm getting rather confused! Any ideas? I'm sure it's something pretty obvious.
Cheers
When you initialize a new Date()
it defaults to the current date. getDate
only refers to the day, not the month or year. Your code worked in September because that was the same as the start month, but since it is now October, you are offsetting from the wrong month.
var updated_date = new Date ();
should be
var updated_date = new Date (2011, 8, 5);
That way you are offsetting from the start of the school year, not today.
Demo: http://jsfiddle.net/aXgk6/
Use this code, and change the base date to the this.week
variable.
I've also applied a change to the part of the code which determines a new week. Otherwise, you would be back near the end of this calender year, asking why your code is broken ;)
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));
var not_new = true;
for (var i = 1; i < 51; i++) {
var week = i * 7;
var updated_date = new Date(this.date);
updated_date.setDate(this.date.getDate() + week);
if (not_new && updated_date.getYear() > this.date.getYear() ) {
not_new = this.num = 0;
}
this.num++;
this.weeks.push(new LEAP.Schedule.week(updated_date, this.num));
}
};
精彩评论