Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Datepicker showing incorrect week number

When using the jQuery UI Datepicker and starting the week on a Sunday, the week numbers are incorrect. For example, 3rd Jan 2016 should be week 1 as all of the dates (3rd to 9th) are in the same year. But as you can see in the screenshot below, the UI shows it as week 53.

enter image description here

Here is the code to render the datepicker:

$("#datepicker" ).datepicker({
    showWeek: true,
    firstDay: 0
});

So nothing special, other than showing the week numbers and starting the week on Sunday instead of Monday (as per default).

Here is a fiddle of the issue: https://jsfiddle.net/vLqabmmz/

like image 508
Cameron Avatar asked Jan 21 '16 13:01

Cameron


4 Answers

Due to the fact that this seems like a bug in jQuery UI. I'm posting the possible answer being the reporting of this as a bug to the jQuery UI team here: https://bugs.jqueryui.com/ticket/14907#ticket

like image 119
Cameron Avatar answered Nov 15 '22 19:11

Cameron


i checked further and iso8601Week() method of jqueryUI is working fine, the problem is in its representation (the layout).

If you extract the value of the week you can see that the standard is followed and is fine. 3-rd is week 53 and after that 4 ... 7 (which is the first Thursday of the year and this is the week number 1). You can check with the code below.

  $(function() {
    $( "#datepicker" ).datepicker({
      showWeek: true,
      onSelect: function(dateText, inst) {
            $(this).val("'Week Number '" + $.datepicker.iso8601Week(new Date(dateText)));
      }
    });
  });
like image 32
Vitaliy Terziev Avatar answered Nov 15 '22 19:11

Vitaliy Terziev


As seen from documentation for calculateWeek here:

This function uses the ISO 8601 definition of a week: weeks start on a Monday and the first week of the year contains January 4.

So comments are right qualifying this as a bug. While this is not resolved yet, you can overcome this easily by setting firstDay to 1 like this:

$("#datepicker" ).datepicker({
    showWeek: true,
    firstDay: 1
});

Week number would be correct, it is easier than rewrite calculateWeek, only difference is that week will start on Monday instead on Sunday. Note that default behavior sets firstDay to 0.

like image 1
RandomCoder Avatar answered Nov 15 '22 17:11

RandomCoder


Here is a display of the jQuery calendar for the first week of 2017 - fully contained in a new year.
Yet, the week number is shown as 52 of year 2016 (wtf). enter image description here

To me it looks like a bug to be corrected, otherwise there is no reason to display the week number.

like image 1
MaxZoom Avatar answered Nov 15 '22 19:11

MaxZoom