From 3d73d0c804a65084b3d92f75717a305f87fc232b Mon Sep 17 00:00:00 2001 From: co-sic Date: Wed, 23 Oct 2024 17:18:56 +0200 Subject: [PATCH] fix: allow date below year 1000 for fromDateWithTimeZone --- src/CalendarDate.ts | 4 +++- test/CalendarDate.test.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/CalendarDate.ts b/src/CalendarDate.ts index 080cd69..fdada31 100644 --- a/src/CalendarDate.ts +++ b/src/CalendarDate.ts @@ -146,7 +146,9 @@ export class CalendarDate { * returns a CalendarDate instance for the supplied Date, using the supplied time zone string */ static fromDateWithTimeZone(date: Date, timeZone: string): CalendarDate { - return new CalendarDate(CalendarDate.getIntlDateTimeFormatter(timeZone).format(date)); + return new CalendarDate( + CalendarDate.getIntlDateTimeFormatter(timeZone).format(date).padStart(10, '0'), + ); } /** diff --git a/test/CalendarDate.test.ts b/test/CalendarDate.test.ts index c5a1842..51bac07 100644 --- a/test/CalendarDate.test.ts +++ b/test/CalendarDate.test.ts @@ -216,6 +216,21 @@ describe('CalendarDate', () => { }); }); + describe('Test of fromDateWithTimeZone', () => { + test('should return correct date for a year < 1000', () => { + // Arrange + const date = new Date(50, 0, 1); + + // Act + const calendarDate = CalendarDate.fromDateWithTimeZone(date, 'America/New_York'); + + // Assert + expect(calendarDate.toFormat('d.M.yyyy')).toBe( + date.toLocaleDateString('de-DE', { timeZone: 'America/New_York' }), + ); + }); + }); + describe('Test of parseString', () => { test('Should throw error if input is not a valid iso string', () => { fc.assert(