setDate(day+1) returns wrong days
Why does this:
startDate.setDate(startDate + 1);
Generates this strange order (broken at the beginning of the next month):
7, 27, 28, 30, 30, **1, 4, 3, 4, 4,** 6, 7, 10, 9, 10, 10, 12,
Update (more code):
renderWeekFromMonday : function(date) {
var dayContainer = $('#day');
// clear div
dayContainer.empty();
// Render days
for (var i = 0; i <= 7; i++) {
// Get mondday day (1-31)
var day = date.getDate();
// Today
var t = new Date();
// Create dayobject for usage inside for loop
var d = new Date(date);
// Render 7 days (1 week)
for (var i = 0; i < 7; i++) {
// New day (+1)
d.setDate(day + i)
console.info(d.g开发者_如何学GoetDate());
// Create html
var span = $("<span>").addClass("calElement").attr("time", d.getTime())
var w = 25;
span.html("<span class=dayNumber>" + d.getDate() + "</span><br>" + this.dayNames[d.getDay()]).css("width",w);
//span.html("<span class=dayNumber>" + d.getDate() + "</span>");
// Append day
dayContainer.append(span);
}
}
},
Just a guess, put perhaps you're looking for:
startDate.setDate(startDate.getDate() + 1);
startDate + 1
doesn't make much sense if startDate
is a Date
object.
After seeing updated code: Your problem is very likely in the fact that you have two nested loops, both of which increment i
. Use a different variable for one of the loops.
Your code looks fine - I'm using FF4 and the date calculations look correct. I've posted the code here for a live example: http://jsfiddle.net/EbNcr/2/
Is there a specific browser or date(s) that you're testing with to get the strange results?
(I wish I could submit this as a comment, but I don't think I have the reputation for that yet...)
Thanks for helping. I just had a simple copy&paste error (two loops). I put the final code into a simple jquery plugin (inspiered by some oder plugins)
var MILLIS_IN_DAY = 86400000;
var MILLIS_IN_WEEK = MILLIS_IN_DAY * 7
jQuery.fn.calendarPicker = function(options) {
// -------------------------- start default option values --------------------------
options.date = new Date();
options.dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
options.firstDayOfWeek = 1;
options.showNavigation = true;
// -------------------------- end default option values --------------------------
var calendar = {
changeDate : function(date) {
// calculate new start date
this.currentFirstDayOfWeek = this._firstDayOfWeek(date);
// render days
for (var i = 0; i < 7; i++) {
// create dayobject
var day = new Date(this.currentFirstDayOfWeek.getTime() + MILLIS_IN_DAY * i);
// render container
var span = $("<span>").addClass("calElement").attr("time", day.getTime())
// render day
span.html("<span class=dayNumber>" + day.getDate() + "</span><br>" + options.dayNames[day.getDay()]);
if (day.getYear() == date.getYear() && date.getMonth() == day.getMonth() && day.getDate() == date.getDate())
span.addClass("today");
if (day.getYear() == date.getYear() && day.getMonth() == date.getMonth() && day.getDate() == date.getDate())
span.addClass("selected");
theDiv.append(span);
// render navigation
if (i == 0 && options.showNavigation) {
var prevBtn = $("<span>").addClass("prev").html("prev");
var self = this;
prevBtn.bind('click', function() { self.prevWeek(); });
theDiv.prepend(prevBtn);
} else if (i == 6 && options.showNavigation) {
var nextBtn = $("<span>").addClass("next").html("next")
var self = this;
nextBtn.bind('click', function() { self.nextWeek(); });
theDiv.append(nextBtn);
}
}
},
/*
* Go to the previous week relative to the currently displayed week
*/
prevWeek : function() {
//minus more than 1 day to be sure we're in previous week - account for daylight savings or other anomolies
var newDate = new Date(this.currentFirstDayOfWeek.getTime() - (MILLIS_IN_WEEK / 6));
this._clearCalendar();
this.changeDate(newDate);
},
/*
* Go to the next week relative to the currently displayed week
*/
nextWeek : function() {
//add 8 days to be sure of being in prev week - allows for daylight savings or other anomolies
var newDate = new Date(this.currentFirstDayOfWeek.getTime() + MILLIS_IN_WEEK + (MILLIS_IN_WEEK / 7));
this._clearCalendar();
this.changeDate(newDate);
},
/*
* returns the date on the first millisecond of the week
*/
_firstDayOfWeek : function(date) {
var midnightCurrentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var adjustedDate = new Date(midnightCurrentDate);
adjustedDate.setDate(adjustedDate.getDate() - this._getAdjustedDayIndex(midnightCurrentDate));
return adjustedDate;
},
_clearCalendar : function() {
theDiv.empty();
},
/*
* gets the index of the current day adjusted based on options (e.g.firstDayofWeek)
*/
_getAdjustedDayIndex : function(date) {
var midnightCurrentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var currentDayOfStandardWeek = midnightCurrentDate.getDay();
var days = [0,1,2,3,4,5,6];
this._rotate(days, this.options.firstDayOfWeek);
return days[currentDayOfStandardWeek];
},
/*
* rotate an array by specified number of places.
*/
_rotate : function(a, p ) {
for (var l = a.length, p = (Math.abs(p) >= l && (p %= l),p < 0 && (p += l),p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p)) {
for (i = l; i > p; x = a[--i],a[i] = a[i - p],a[i - p] = x);
}
return a;
},
};
// Pass options
calendar.options = options;
// build the calendar on the first element in the set of matched elements.
var theDiv = this.eq(0);//$(this);
theDiv.addClass("calBox");
// empty the div
theDiv.empty();
// entry point
calendar.changeDate(options.date);
return calendar;
};
精彩评论