JosephMajaseSithole / datejs

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

getISOWeek() not returning correct values #52

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. var d1=new Date(2005, 0, 1);
2. document.write(d1.getISOWeek());

What is the expected output? What do you see instead?
Since 2005-01-01 is a Saturday, and since that week has less than 4 days in
the new year, the getISOWeek function should return '53' (53rd week of the
previous year), but instead it returns '01'.

What version of the product are you using? On what operating system?
@version: 1.0 Alpha-1, @date: 2008-05-13, for all 6 files. Windows XP, SP2.

Please provide any additional information below.
getISOWeek should return '53' for the week from 2004-12-27 Mon to
2005-01-02 Sun. getISOWeek should also return '01' for the week from
2005-01-03 Mon to 2005-01-09 Mon. However, instead getISOWeek seems to
return weeks based on Fridays, i.e. it returns '01' for the range of
2004-12-31 Fri to 2005-01-06 Thu.

-George Hernandez

Original issue reported on code.google.com by georgelhernandez on 2 Oct 2008 at 8:26

GoogleCodeExporter commented 8 years ago
Hi George,

I tested your sample on a couple different computers with different culture 
settings
and different timezones and each one returned week "53".

Can you confirm that you are using the latest code from svn 
(http://www.datejs.com/svn/)?

Thanks for the code samples.

Original comment by geoff%co...@gtempaccount.com on 2 Oct 2008 at 8:32

GoogleCodeExporter commented 8 years ago
Yes. I've tried the following variations of the 2008 code but I still get the 
same:
1. date.js
2. date-en-US.js
3. Including the 6 component files separately and in order.

(The download has 2007 code and has getWeekOfYear but not getWeek or 
getISOWeek.)

BTW: The ISO 8601 standard for week does not involve converting to UTC.

FYI: Here's what I use for getting the week. It covers 5 different kinds of 
weeks and
is much cleaner than Claus's code. To fit your methodology, you might do 
something
like eliminate the parameter and break it up in 5 distinct methods.

Date.prototype.getWeekOfYear = function(flag) {
    var j1 = new Date(this.getFullYear(), 0, 1);
    var d_dow, j1_dow, d1w1;
    switch (flag) {
    case 'mon_iso':
        d_dow = this.getDay() || 7;
        j1_dow = j1.getDay() || 7;
        d1w1 = new Date(j1.getTime() + (j1_dow>4 ? 8-j1_dow :
-(j1_dow-1))*1000*60*60*24);
        break;
    case 'mon_jan':
        d_dow = this.getDay() || 7;
        j1_dow = j1.getDay() || 7;
        d1w1 = new Date(j1.getTime() - j1_dow*1000*60*60*24);
        break;
    case 'mon_mon':
        d_dow = this.getDay() || 7;
        j1_dow = j1.getDay() || 7;
        d1w1 = new Date(j1.getTime() + (j1_dow==1 ? 0 : 8-j1_dow)*1000*60*60*24);
        break;
    case 'sun_sun':
        d_dow = this.getDay();
        j1_dow = j1.getDay();
        d1w1 = new Date(j1.getTime() + (j1_dow ? 7-j1_dow : 0)*1000*60*60*24);
        break;
    case 'sun_jan':
    default:
        d_dow = this.getDay();
        j1_dow = j1.getDay();
        d1w1 = new Date(j1.getTime() - j1_dow*1000*60*60*24);
    }
    var daysDiff = Math.floor((this.getTime() - d1w1) /1000/60/60/24);
    if (flag=='mon_iso' && j1_dow>4) {
        var endOfLastYear = new Date(this.getFullYear()-1, 11, 31);
        var wk = endOfLastYear.getWeekOfYear(1, 'iso');
    } else var wk = Math.floor((daysDiff/7)+1);
    return wk;
}

-George Hernandez

Original comment by georgelhernandez on 3 Oct 2008 at 5:27

GoogleCodeExporter commented 8 years ago
Oops! I had pasted in an old copy. Here's the current:

Date.prototype.getWeek = function(flag) {
    var j1 = new Date(this.getFullYear(), 0, 1);
    var d_dow, j1_dow, d1w1;
    switch (flag) {
    case 'mon_iso':
        d_dow = this.getDay() || 7;
        j1_dow = j1.getDay() || 7;
        d1w1 = new Date(j1.getTime() + (j1_dow>4 ? 8-j1_dow :
-(j1_dow-1))*1000*60*60*24);
        break;
    case 'mon_jan':
        d_dow = this.getDay() || 7;
        j1_dow = j1.getDay() || 7;
        d1w1 = new Date(j1.getTime() - (j1_dow-1)*1000*60*60*24);
        break;
    case 'mon_mon':
        d_dow = this.getDay() || 7;
        j1_dow = j1.getDay() || 7;
        d1w1 = new Date(j1.getTime() + (j1_dow==1 ? 0 : 8-j1_dow)*1000*60*60*24);
        break;
    case 'sun_sun':
        d_dow = this.getDay();
        j1_dow = j1.getDay();
        d1w1 = new Date(j1.getTime() + (j1_dow==0 ? 0 : 7-j1_dow)*1000*60*60*24);
        break;
    case 'sun_jan':
    default:
        d_dow = this.getDay();
        j1_dow = j1.getDay();
        d1w1 = new Date(j1.getTime() - (j1_dow)*1000*60*60*24);
    }
    var daysDiff = Math.floor((this.getTime() - d1w1.getTime()) /1000/60/60/24);
    var wk = Math.floor((daysDiff/7)+1);
    if (flag=='mon_iso' && wk==0) {
        var endOfLastYear = new Date(this.getFullYear()-1, 11, 31);
        wk =  endOfLastYear.getWeek('mon_iso');
    }
    return wk.pad(2); //Assumes Number.prototype.pad in place
}

-George Hernandez

Original comment by georgelhernandez on 3 Oct 2008 at 9:20

GoogleCodeExporter commented 8 years ago
OK: In the 2008-05-13 version of Datejs, getISOWeek() correctly returns "53" for
2005-01-01. Consider this issue closed.

BTW: Nice work!

-George Hernandez

Original comment by georgelhernandez on 10 Oct 2008 at 4:35

GoogleCodeExporter commented 8 years ago
Why hasn't this issue been closed?

Original comment by harris...@gmail.com on 21 Jul 2010 at 1:52

GoogleCodeExporter commented 8 years ago
because Geoff has abandoned the project.

I've made a lot of fixes and progress on the issues here (as well as other 
changes and features) on my fork here: https://github.com/abritinthebay/datejs/

Original comment by gwildsm...@bleacherreport.com on 14 Sep 2013 at 12:50