pingjiang / datejs

Automatically exported from code.google.com/p/datejs
Other
0 stars 0 forks source link

[FIXED] getWeekOfYear is buggy and does not conform to ISO 8601:1988 (for locales using that flavor) #31

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Iterating from 1 st of january with monday as start of week will
give buggy results (0, 52, 1, 2, 3) and weeks will be one off from 
the ISO version. 

  dateiterator = new Date(2008,01-1,01);
  end   = new Date(2008,05-1,31);
  while (dateiterator < end)
  {
    alert(dateiterator + '\n' + dateiterator.getWeekOfYear());
    dateiterator.moveToDayOfWeek(1);
    i++;
  }

Definition: A week is identified by its number in a given year. A week
begins with a Monday, and the first week of a year is the one which
includes the first Thursday, or equivalently the one which includes January 4. 

Original issue reported on code.google.com by patrik0c...@gmail.com on 27 Feb 2008 at 9:19

GoogleCodeExporter commented 9 years ago
Totally agree. I'm going to rewrite .getWeekOfYear to follow ISO 8601. This 
should 
be added very soon.

I was not aware of the bug you noted above. Thanks for pointing it out.

Original comment by geoff%co...@gtempaccount.com on 28 Feb 2008 at 5:26

GoogleCodeExporter commented 9 years ago
The .getWeekOfYear function has been removed and replaced with the following 
two 
functions.

1. .getWeek() - Returns the week number. The first week of the year is the week 
which contains the first Thursday. Monday is considered the first day of the 
week.

Example

Date.today().getWeek();

2. .getISOWeek() - Converts the date to it's UTC value before getting week 
number.

Example

Date.today().getISOWeek();

I also added a .setWeek() function which moves the date to Monday of the week 
set.

Example

Date.today().setWeek(1); // set to the Monday of the first week of year

I also added "week" as a config option in .set().

Example

Date.today().set({week: 1}); // set to first week of year

Hope this helps.

Original comment by geoff%co...@gtempaccount.com on 6 Apr 2008 at 9:56