Hi,
I'm using datepicker.iso8601Week() to calculate the week from a selected date (in a jQuery UI datepicker).
According to iso8601Week, a week goes from Tuesday-Monday, and I need it to go from Monday-Sunday. How can this be accomplished?
Hi,
I'm using datepicker.iso8601Week() to calculate the week from a selected date (in a jQuery UI datepicker).
According to iso8601Week, a week goes from Tuesday-Monday, and I need it to go from Monday-Sunday. How can this be accomplished?
Start on Monday
$('#date').DatePicker({
starts: 1
});
Start on Sunday
$('#date').DatePicker({
starts: 0
});
Hope this can help you.
Sorry I did not have enough time to do the detailed unit testing.
function getDateRangeOfWeek(week){
var date = new Date();
var currentWeek = $.datepicker.iso8601Week(date);
date.setDate(date.getDate() - date.getDay() + 1 + (7 * (week - currentWeek)));
var result = [];
for(var i = 0;i < 7;i++){
var d = new Date();
d.setDate(date.getDate() + i)
result[i] = d;
}
return result;
};
According to the JQuery UI Datepicker documentation weeks start on mondays for the iso8601Week
function, which is the only sane thing if it is supposed to follow the ISO 8601 standard. If you are getting different behaviour I would consider it to be a bug. You can take a look at the implementation here.
Getting datepiker work with Drupal Calendar I found out that per week filtering works incorrectly as datepicker.iso8601Week function returned invalid values. Well, my week started on Wednesday in spite of all facts like official docs :) and that was the problem. I finished up with the following trick:
var date = $.datepicker.iso8601Week(new Date(dateText));
var dateDay = $.datepicker.formatDate('D', new Date(dateText));
if (dateDay == 'Mon' || dateDay == 'Sun') {
date = date+1;
}
That piece of code returns correct week number for Mon and Sun (as well as all other days) upon click. If you use datepicker as inline filter interface, you may also want to preserve the clicked date and show up the calendar starting from clicked position. I do it through URLs of the exact length with month name at the end:
var setMon = window.location.pathname.slice(20);
var setDay = window.location.pathname.slice(17,19);
var setYear = window.location.pathname.slice(8,12);
$('.datep').datepicker({
defaultDate: $.datepicker.parseDate("dd MM y", setDay+" "+setMon+" "+setYear)
});
Hope that can help others as I spent god damn much time on figuring out how to fix the week number. Cheers.