diff --git a/.changeset/cyan-cooks-nail.md b/.changeset/cyan-cooks-nail.md new file mode 100644 index 0000000000..656cf3a413 --- /dev/null +++ b/.changeset/cyan-cooks-nail.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: consider `valueOf` in the reactive methods of `SvelteDate` diff --git a/packages/svelte/src/reactivity/date.js b/packages/svelte/src/reactivity/date.js index 9eb0cc3521..2d8de624bc 100644 --- a/packages/svelte/src/reactivity/date.js +++ b/packages/svelte/src/reactivity/date.js @@ -30,7 +30,7 @@ export class SvelteDate extends Date { ); for (const method of methods) { - if (method.startsWith('get') || method.startsWith('to')) { + if (method.startsWith('get') || method.startsWith('to') || method === 'valueOf') { // @ts-ignore proto[method] = function (...args) { // don't memoize if there are arguments diff --git a/packages/svelte/src/reactivity/date.test.ts b/packages/svelte/src/reactivity/date.test.ts index d07b6780c6..87bfde41c8 100644 --- a/packages/svelte/src/reactivity/date.test.ts +++ b/packages/svelte/src/reactivity/date.test.ts @@ -588,6 +588,30 @@ test('Date.toLocaleString', () => { cleanup(); }); +test('Date.valueOf', () => { + const date = new SvelteDate(initial_date); + + const log: any = []; + + const cleanup = effect_root(() => { + render_effect(() => { + log.push(date.valueOf()); + }); + }); + + flushSync(); + + assert.deepEqual(log, [initial_date.valueOf()]); + + flushSync(() => { + date.setTime(date.getTime() + 10); + }); + + assert.deepEqual(log, [initial_date.valueOf(), new Date(initial_date.getTime() + 10).valueOf()]); + + cleanup(); +}); + test('Date.instanceOf', () => { assert.equal(new SvelteDate() instanceof Date, true); });