1

We're using this implementation of calculating ISO 8601 week numbers in Javascript. This has worked great for us in North America, but we recently starting hearing bug reports from our users in Australia that we're showing the wrong week number. Sure enough changing our timezone to AEST starting getting us the wrong week numbers. Here's the output:

AEST Output

var date = new Date();
date; // -> Fri May 12 2017 04:43:15 GMT+1000 (AEST)
var target = new Date(date.valueOf());
target; // -> Fri May 12 2017 04:05:13 GMT+1000 (AEST)
target.setDate(target.getDate() - ((date.getDay() + 6) % 7) + 3);
target; // -> Thu May 11 2017 04:05:13 GMT+1000 (AEST)
var thisThursday = target.valueOf();
thisThursday; // -> 1494439513182
target.setMonth(0, 1);
target; // -> Sun Jan 01 2017 04:05:13 GMT+1100 (AEDT)
if (target.getDay() !== 4) {
  target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}
target; // -> Thu Jan 05 2017 04:05:13 GMT+1100 (AEDT)
var weekNumber = 1 + Math.ceil((thisThursday - target) / 604800000);
weekNumber; // -> 20 [Should be 19]

PDT Output

var date = new Date();
date; // -> Thu May 11 2017 11:56:36 GMT-0700 (PDT)
var target = new Date(date.valueOf());
target; // -> Thu May 11 2017 11:56:36 GMT-0700 (PDT)
target.setDate(target.getDate() - ((date.getDay() + 6) % 7) + 3);
target; // -> Thu May 11 2017 11:56:36 GMT-0700 (PDT)
var thisThursday = target.valueOf();
thisThursday; // -> 1494528996804
target.setMonth(0, 1);
target; // -> Sun Jan 01 2017 11:56:36 GMT-0800 (PST)
if (target.getDay() !== 4) {
  target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}
target; // -> Thu Jan 05 2017 11:56:36 GMT-0800 (PST)
var weekNumber = 1 + Math.ceil((thisThursday - target) / 604800000);
weekNumber; // -> 19 [Correct!]

This is pretty bizarre for us and would love some help figuring out what could be going wrong with the different timezones.

Eric Koslow
  • 2,004
  • 2
  • 21
  • 33
  • well are you storing the dates as UTC or EST? – epascarello May 11 '17 at 19:07
  • Could be related to daylight saving time change for AEST that happened on april which is between your two dates – juvian May 11 '17 at 19:08
  • Hey @epascarello we're actually not storing any dates here. This is happening in people's browsers when they visit our webpage. – Eric Koslow May 11 '17 at 19:08
  • I would suggest using UTC values rather than local. IOW use [`getUTCDate()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDate) instead of `getDate()`, etc. – Heretic Monkey May 11 '17 at 19:08
  • @MikeMcCaughan We'll try that, but subtracting the current Thursday from the first Thursday shouldn't be affected by timezone right? As long as they are in the same timezone. – Eric Koslow May 11 '17 at 19:11
  • 1
    You might take a look at the answers to [this question](http://stackoverflow.com/a/6117889/215552). It mentions PHP but it's really about JavaScript. – Heretic Monkey May 11 '17 at 19:29
  • @MikeMcCaughan Yeah we saw that thread. But while that function works for this year in both timezones it doesn't work for year's like 2014. – Eric Koslow May 11 '17 at 19:44
  • @MikeMcCaughan Nevermind you're right. The 2014 was a red herring. Turns out it using our time in 2014 was making the AEST Monday and PT Sunday. I would still like to understand why this version doesn't work. But for now we're switching to that other function. Thanks! – Eric Koslow May 11 '17 at 19:56

0 Answers0