diff --git a/pkg/base1/test-timeformat.ts b/pkg/base1/test-timeformat.ts index 4cca97dd02e..ba926142271 100644 --- a/pkg/base1/test-timeformat.ts +++ b/pkg/base1/test-timeformat.ts @@ -124,21 +124,24 @@ QUnit.test("firstDayOfWeek", assert => { assert.equal(timeformat.firstDayOfWeek(), 1); }); -// FIXME: This test is currently time zone dependent; parseShortDate() always -// interprets its argument as midnight UTC instead of local time; so it's even -// off by a day for any TZ east of UTC. -QUnit.skip("parsing", assert => { +QUnit.test("parsing", assert => { cockpit.language = "en"; const en = timeformat.parseShortDate("1/20/2024"); - assert.equal(en.toISOString(), "2024-01-20T00:00:00.000Z"); + assert.equal(en.getDate(), 20); + assert.equal(en.getMonth(), 0); // yes, starting from 0 + assert.equal(en.getFullYear(), "2024"); cockpit.language = "en_GB"; const engb = timeformat.parseShortDate("20/01/2024"); - assert.equal(engb.toISOString(), "2024-01-20T00:00:00.000Z"); + assert.equal(engb.getDate(), 20); + assert.equal(engb.getMonth(), 0); // yes, starting from 0 + assert.equal(engb.getFullYear(), "2024"); cockpit.language = "de"; const de = timeformat.parseShortDate("20.01.2024"); - assert.equal(de.toISOString(), "2024-01-20T00:00:00.000Z"); + assert.equal(de.getDate(), 20); + assert.equal(de.getMonth(), 0); // yes, starting from 0 + assert.equal(de.getFullYear(), "2024"); }); QUnit.start(); diff --git a/pkg/lib/timeformat.ts b/pkg/lib/timeformat.ts index b644afb560d..bdae84be8cf 100644 --- a/pkg/lib/timeformat.ts +++ b/pkg/lib/timeformat.ts @@ -50,12 +50,10 @@ export const distanceToNow = (t: Time, addSuffix?: boolean): string => formatDis // Parse a string localized date like 30.06.21 to a Date Object export function parseShortDate(dateStr: string): Date { - const parsed = parse(dateStr, dateShortFormat(), new Date()); - - // Strip time which may cause bugs in calendar - const time = parsed.getTime(); - const timePortion = time % (3600 * 1000 * 24); - return new Date(time - timePortion); + // strip off time (i.e. set to midnight), to avoid confusing calendar widgets + const now = new Date(); + const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + return parse(dateStr, dateShortFormat(), today); } /***