diff --git a/CHANGELOG.md b/CHANGELOG.md index 450309d..dfbe93d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ Compare with [last published version](https://github.com/meduzen/datetime-attrib ### Fixed -- Fix incorrect year for `datetime(date, 'week')` when the week started the previous year. For example, `2021-01-01` is a Friday and its week belongs to 2020 (as [per spec](./README.md#weeknumber). In that case, the output was `2021-53` instead of `2020-53`. +- Fix incorrect year for `datetime(date, 'week')` when the week started the previous year. For example, `2021-01-01` is a Friday and its week belongs to 2020 (as [per spec](./README.md#weeknumber)). In that case, the output was `2021-53` instead of `2020-53`. ### Improved diff --git a/src/datetime-class.js b/src/datetime-class.js index f9bee65..7394f42 100644 --- a/src/datetime-class.js +++ b/src/datetime-class.js @@ -1,8 +1,16 @@ import { MILLISECONDS_PER_WEEK } from './utils/const.js' import { datetime, weekNumber } from './index.js' +import { getNormalizeDay } from './utils/date.js' export class DateTime extends Date { + /** + * Get the day index of the week, except Sunday is 7 instead of 0. + * + * @returns {number} + */ + getNormalisedDay = () => getNormalizeDay(this) + /** * Returns the week of the year (`1`–`53`) of the specified date according to * local time, as defined by the WHATWG and the ISO-8601 specs: diff --git a/src/datetime-class.test.js b/src/datetime-class.test.js index c5c8748..2307aa8 100644 --- a/src/datetime-class.test.js +++ b/src/datetime-class.test.js @@ -45,6 +45,8 @@ describe('DateTime class', () => { }) }) + describe.todo('.getNormalisedDay()', () => {}) + describe('.getWeek()', () => { test('returns the week number', () => { diff --git a/src/datetime.test.js b/src/datetime.test.js index f9e5c9d..8e33489 100644 --- a/src/datetime.test.js +++ b/src/datetime.test.js @@ -106,7 +106,7 @@ describe('datetime', () => { expect(datetime(january19th, 'week')).toBe('2021-W03') }) - // 1st day of the month is after Thurdsay + // 1st day of the month is after Thurdsay, but it’s not in January test('week on 2021-03-01 is 2021-W17', () => { const march1st2021 = new Date(2021, 4, 1) expect(getNormalizeDay(march1st2021)).toBeGreaterThan(4) diff --git a/src/utils/date.js b/src/utils/date.js index 1ca8a51..ec38e03 100644 --- a/src/utils/date.js +++ b/src/utils/date.js @@ -35,7 +35,7 @@ export function daysBetween(date, furtherDate) { * @returns {number} */ export function weekNumber(date) { - const dayIndex = getNormalizeDay(date) // normalize index because Sunday == 0 + const dayIndex = getNormalizeDay(date) const sameWeekThursday = new Date(date) sameWeekThursday.setDate(date.getDate() + 4 - dayIndex) @@ -47,7 +47,7 @@ export function weekNumber(date) { } /** - * Get the day index of the week, except Sunday is 7 instead of 0. + * Get the day index of the week, but Sunday is now 7 instead of 0. * * @param {Date} date * @returns {number}