我们正在使用this implementation在Javascript中计算ISO 8601周数。这在北美对我们来说非常有用,但我们最近开始听取澳大利亚用户的错误报告,我们显示错误的周数。果然将我们的时区改为AEST
,开始给我们错误的周数。这是输出:
AEST输出
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输出
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!]
这对我们来说非常奇怪,并希望能帮助找出不同时区可能出现的问题。