Skip to content

Commit

Permalink
Add helper to get the normalized day index on DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
meduzen committed Apr 12, 2024
1 parent cb768ad commit c9dee7a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/datetime-class.js
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/datetime-class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('DateTime class', () => {
})
})

describe.todo('.getNormalisedDay()', () => {})

describe('.getWeek()', () => {

test('returns the week number', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/datetime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand Down

0 comments on commit c9dee7a

Please sign in to comment.