Skip to content

Commit 49d489d

Browse files
committed
#1144@trivial: Updates Date ISO week function to a more battle tested.
1 parent f26b084 commit 49d489d

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

packages/happy-dom/src/nodes/html-input-element/HTMLInputElementDateUtility.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@ export default class HTMLInputElementDateUtility {
55
/**
66
* Returns iso week number from given date
77
*
8+
* @see https://stackoverflow.com/a/6117889
89
* @param date Date or number.
910
* @returns Iso-week string.
1011
*/
1112
public static dateIsoWeek(date: Date | number): string {
12-
date = new Date(date);
13-
const day = (date.getUTCDay() + 6) % 7;
14-
date.setUTCDate(date.getUTCDate() - day + 3);
15-
const firstThursday = date.getTime();
16-
date.setUTCMonth(0, 1);
17-
if (date.getUTCDay() !== 4) {
18-
date.setUTCMonth(0, 1 + ((4 - date.getUTCDay() + 7) % 7));
19-
}
20-
return (
21-
date.getUTCFullYear() +
22-
'-W' +
23-
String(1 + Math.ceil((firstThursday - date.getTime()) / 604800000)).padStart(2, '0')
13+
const parsedDate = typeof date === 'number' ? new Date(date) : date;
14+
// Copy date so don't modify original
15+
const newDate = new Date(
16+
Date.UTC(parsedDate.getFullYear(), parsedDate.getMonth(), parsedDate.getDate())
17+
);
18+
// Set to nearest Thursday: current date + 4 - current day number
19+
// Make Sunday's day number 7
20+
newDate.setUTCDate(newDate.getUTCDate() + 4 - (newDate.getUTCDay() || 7));
21+
// Get first day of year
22+
const yearStart = new Date(Date.UTC(newDate.getUTCFullYear(), 0, 1));
23+
// Calculate full weeks to nearest Thursday
24+
const weekNo = Math.ceil(
25+
((<number>(<unknown>newDate) - <number>(<unknown>yearStart)) / 86400000 + 1) / 7
2426
);
27+
return `${newDate.getUTCFullYear()}-W${weekNo < 10 ? '0' : ''}${weekNo}`;
2528
}
29+
2630
/**
2731
* Returns a date object for monday of given iso week string (\d\d\d\d-W\d\d)
2832
*

packages/happy-dom/test/nodes/html-input-element/HTMLInputDateUtility.test.ts renamed to packages/happy-dom/test/nodes/html-input-element/HTMLInputElementDateUtility.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
22
import HTMLInputElementDateUtility from '../../../src/nodes/html-input-element/HTMLInputElementDateUtility.js';
33

44
describe('HTMLInputElementDateUtility', () => {
5-
describe('dateToIsoWeek()', () => {
5+
describe('dateIsoWeek()', () => {
66
it('Returns the ISO week number', () => {
77
expect(HTMLInputElementDateUtility.dateIsoWeek(new Date('2021-01-01'))).toBe('2020-W53');
88
expect(HTMLInputElementDateUtility.dateIsoWeek(new Date('2021-01-03'))).toBe('2020-W53');
@@ -35,7 +35,7 @@ describe('HTMLInputElementDateUtility', () => {
3535
});
3636
});
3737

38-
describe('IsoWeekToDate()', () => {
38+
describe('isoWeekDate()', () => {
3939
it('Returns the ISO week number', () => {
4040
expect(HTMLInputElementDateUtility.isoWeekDate('2020-W53')).toEqual(
4141
new Date('2020-12-28T00:00Z')

0 commit comments

Comments
 (0)