Skip to content

Commit c9dee7a

Browse files
committed
Add helper to get the normalized day index on DateTime
1 parent cb768ad commit c9dee7a

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Compare with [last published version](https://github.com/meduzen/datetime-attrib
1010

1111
### Fixed
1212

13-
- 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`.
13+
- 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`.
1414

1515
### Improved
1616

src/datetime-class.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { MILLISECONDS_PER_WEEK } from './utils/const.js'
22
import { datetime, weekNumber } from './index.js'
3+
import { getNormalizeDay } from './utils/date.js'
34

45
export class DateTime extends Date {
56

7+
/**
8+
* Get the day index of the week, except Sunday is 7 instead of 0.
9+
*
10+
* @returns {number}
11+
*/
12+
getNormalisedDay = () => getNormalizeDay(this)
13+
614
/**
715
* Returns the week of the year (`1`–`53`) of the specified date according to
816
* local time, as defined by the WHATWG and the ISO-8601 specs:

src/datetime-class.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ describe('DateTime class', () => {
4545
})
4646
})
4747

48+
describe.todo('.getNormalisedDay()', () => {})
49+
4850
describe('.getWeek()', () => {
4951

5052
test('returns the week number', () => {

src/datetime.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('datetime', () => {
106106
expect(datetime(january19th, 'week')).toBe('2021-W03')
107107
})
108108

109-
// 1st day of the month is after Thurdsay
109+
// 1st day of the month is after Thurdsay, but it’s not in January
110110
test('week on 2021-03-01 is 2021-W17', () => {
111111
const march1st2021 = new Date(2021, 4, 1)
112112
expect(getNormalizeDay(march1st2021)).toBeGreaterThan(4)

src/utils/date.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function daysBetween(date, furtherDate) {
3535
* @returns {number}
3636
*/
3737
export function weekNumber(date) {
38-
const dayIndex = getNormalizeDay(date) // normalize index because Sunday == 0
38+
const dayIndex = getNormalizeDay(date)
3939

4040
const sameWeekThursday = new Date(date)
4141
sameWeekThursday.setDate(date.getDate() + 4 - dayIndex)
@@ -47,7 +47,7 @@ export function weekNumber(date) {
4747
}
4848

4949
/**
50-
* Get the day index of the week, except Sunday is 7 instead of 0.
50+
* Get the day index of the week, but Sunday is now 7 instead of 0.
5151
*
5252
* @param {Date} date
5353
* @returns {number}

0 commit comments

Comments
 (0)