diff --git a/packages/happy-dom/src/nodes/html-input-element/HTMLInputElementDateUtility.ts b/packages/happy-dom/src/nodes/html-input-element/HTMLInputElementDateUtility.ts
index 0187c1a4a..7ae7c33bd 100644
--- a/packages/happy-dom/src/nodes/html-input-element/HTMLInputElementDateUtility.ts
+++ b/packages/happy-dom/src/nodes/html-input-element/HTMLInputElementDateUtility.ts
@@ -5,24 +5,28 @@ export default class HTMLInputElementDateUtility {
/**
* Returns iso week number from given date
*
+ * @see https://stackoverflow.com/a/6117889
* @param date Date or number.
* @returns Iso-week string.
*/
public static dateIsoWeek(date: Date | number): string {
- date = new Date(date);
- const day = (date.getUTCDay() + 6) % 7;
- date.setUTCDate(date.getUTCDate() - day + 3);
- const firstThursday = date.getTime();
- date.setUTCMonth(0, 1);
- if (date.getUTCDay() !== 4) {
- date.setUTCMonth(0, 1 + ((4 - date.getUTCDay() + 7) % 7));
- }
- return (
- date.getUTCFullYear() +
- '-W' +
- String(1 + Math.ceil((firstThursday - date.getTime()) / 604800000)).padStart(2, '0')
+ const parsedDate = typeof date === 'number' ? new Date(date) : date;
+ // Copy date so don't modify original
+ const newDate = new Date(
+ Date.UTC(parsedDate.getFullYear(), parsedDate.getMonth(), parsedDate.getDate())
+ );
+ // Set to nearest Thursday: current date + 4 - current day number
+ // Make Sunday's day number 7
+ newDate.setUTCDate(newDate.getUTCDate() + 4 - (newDate.getUTCDay() || 7));
+ // Get first day of year
+ const yearStart = new Date(Date.UTC(newDate.getUTCFullYear(), 0, 1));
+ // Calculate full weeks to nearest Thursday
+ const weekNo = Math.ceil(
+ (((newDate) - (yearStart)) / 86400000 + 1) / 7
);
+ return `${newDate.getUTCFullYear()}-W${weekNo < 10 ? '0' : ''}${weekNo}`;
}
+
/**
* Returns a date object for monday of given iso week string (\d\d\d\d-W\d\d)
*
diff --git a/packages/happy-dom/test/nodes/html-input-element/HTMLInputDateUtility.test.ts b/packages/happy-dom/test/nodes/html-input-element/HTMLInputElementDateUtility.test.ts
similarity index 98%
rename from packages/happy-dom/test/nodes/html-input-element/HTMLInputDateUtility.test.ts
rename to packages/happy-dom/test/nodes/html-input-element/HTMLInputElementDateUtility.test.ts
index e2b4bcb34..72777fdc0 100644
--- a/packages/happy-dom/test/nodes/html-input-element/HTMLInputDateUtility.test.ts
+++ b/packages/happy-dom/test/nodes/html-input-element/HTMLInputElementDateUtility.test.ts
@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
import HTMLInputElementDateUtility from '../../../src/nodes/html-input-element/HTMLInputElementDateUtility.js';
describe('HTMLInputElementDateUtility', () => {
- describe('dateToIsoWeek()', () => {
+ describe('dateIsoWeek()', () => {
it('Returns the ISO week number', () => {
expect(HTMLInputElementDateUtility.dateIsoWeek(new Date('2021-01-01'))).toBe('2020-W53');
expect(HTMLInputElementDateUtility.dateIsoWeek(new Date('2021-01-03'))).toBe('2020-W53');
@@ -35,7 +35,7 @@ describe('HTMLInputElementDateUtility', () => {
});
});
- describe('IsoWeekToDate()', () => {
+ describe('isoWeekDate()', () => {
it('Returns the ISO week number', () => {
expect(HTMLInputElementDateUtility.isoWeekDate('2020-W53')).toEqual(
new Date('2020-12-28T00:00Z')