diff --git a/server/src/utils/date.spec.ts b/server/src/utils/date.spec.ts index 476b5cb526..36fdef1adc 100644 --- a/server/src/utils/date.spec.ts +++ b/server/src/utils/date.spec.ts @@ -1,5 +1,6 @@ +import { Settings } from 'luxon'; import { asDateString, asDateTimeString } from 'src/utils/date'; -import { describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it } from 'vitest'; describe('asDateString', () => { it('should return null for null input', () => { @@ -10,11 +11,20 @@ describe('asDateString', () => { expect(asDateString('2000-01-15')).toBe('2000-01-15'); }); - it('should return the local calendar date, not the UTC date', () => { - const date = new Date(2000, 0, 15); // 15 Jan 2000, local midnight + afterEach(() => { + Settings.defaultZone = 'system'; + }); + + it('should return the UTC calendar date', () => { + const date = new Date('2000-01-15'); // parsed as UTC midnight, like a database `date` column expect(asDateString(date)).toBe('2000-01-15'); }); + it('should not shift the date when the server runs in a timezone behind UTC', () => { + Settings.defaultZone = 'America/Chicago'; + expect(asDateString(new Date('1994-09-01'))).toBe('1994-09-01'); + }); + it('should correctly pad years with a leading 0', () => { expect(asDateString(new Date('280-12-12'))).toBe('0280-12-12'); }); diff --git a/server/src/validation.ts b/server/src/validation.ts index f8de2a68ff..0ce78eccd0 100644 --- a/server/src/validation.ts +++ b/server/src/validation.ts @@ -161,7 +161,7 @@ export const isoDateToDate = z z.date(), { decode: (isoString) => new Date(isoString), - encode: (date) => DateTime.fromJSDate(date).toFormat('yyyy-MM-dd'), + encode: (date) => DateTime.fromJSDate(date, { zone: 'utc' }).toFormat('yyyy-MM-dd'), }, ) .meta({ example: '2024-01-01' });