diff --git a/src/util/date.js b/src/util/date.js index bcec7d9..102a64d 100644 --- a/src/util/date.js +++ b/src/util/date.js @@ -2,8 +2,15 @@ import { dayOfYear, weekNumber } from 'weeknumber'; +const MINUTE = 60000; +const WEEK = 604800000; + const weekday = date => (date.getDay() + 6) % 7 + 1; +const weekEpoch = new Date(1970, 0, -2); // monday before unix epoch in local timezone +const weekOfEpoch = date => + Math.abs(Math.floor((date - weekEpoch + (weekEpoch.getTimezoneOffset() - date.getTimezoneOffset()) * MINUTE) / WEEK)); + function modify (date, unit, value) { switch (unit) { case 'Y': @@ -48,6 +55,7 @@ const util = { Y: date => date.getMonth() + 1 }, W: { + E: weekOfEpoch, Y: weekNumber }, D: { diff --git a/test/util.js b/test/util.js index 0bf2254..3f6f6ac 100644 --- a/test/util.js +++ b/test/util.js @@ -49,6 +49,7 @@ describe('util', function () { assert.strictEqual(util.date.get.D.W(new Date(2014, 1, 9)), 7); assert.strictEqual(util.date.get.W.Y(new Date(2018, 6, 1)), 26); assert.strictEqual(util.date.get.W.Y(new Date(2021, 0, 1)), 53); + assert.strictEqual(util.date.get.W.E(new Date(1970, 0, 1)), 0); }); }); });