From cd836532419338b96539260de9a60bc9d6c67b50 Mon Sep 17 00:00:00 2001 From: Goldloli <39011904+Goldloli@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:52:18 +0800 Subject: [PATCH] fix(datetime): emit date-only value for presentations without a time picker Confirming a selection with no initial value emitted a full timestamp (e.g. 2026-09-01T16:19:00) where the time was the device clock when the component loaded. The default parts now drop the time fields for presentations that do not render a time picker, so the emitted value matches the date-only format already used when a date-only value is passed. BREAKING CHANGE: ion-datetime now emits date-only values for the date, month, year, and month-year presentations when confirming a selection without an initial value. Applications relying on the time portion should use the date-time or time-date presentations. --- BREAKING.md | 1 + core/src/components/datetime/datetime.tsx | 15 ++++++- .../datetime/test/basic/datetime.e2e.ts | 45 +++++++++++++++++++ .../components/datetime/utils/manipulation.ts | 16 +++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/BREAKING.md b/BREAKING.md index 2322b223402..653c3a92791 100644 --- a/BREAKING.md +++ b/BREAKING.md @@ -118,6 +118,7 @@ This section details the desktop browser and mobile platform versions that are s

Datetime

- The `ion-buttons` component has been removed from the internal implementation of `ion-datetime` and is no longer required when passing custom buttons to the `slot="buttons"`. When providing custom buttons, use a `div` element instead of `ion-buttons`. While existing code using `ion-buttons` may continue to work visually, future updates to the `ion-buttons` component may cause any styles you rely on to break. +- Confirming a selection without an initial `value` no longer includes the current time for presentations that do not render a time picker (`date`, `month`, `year`, and `month-year`). These presentations emitted a full timestamp (e.g. `2026-09-01T16:19:00`) where the time was the device clock when the component loaded. They now emit date-only values (e.g. `2026-09-01`), matching the behavior when a date-only `value` is passed. Applications that relied on the time portion should use the `date-time` or `time-date` presentations.

Grid

diff --git a/core/src/components/datetime/datetime.tsx b/core/src/components/datetime/datetime.tsx index f330ba3b651..c8103bf7d99 100644 --- a/core/src/components/datetime/datetime.tsx +++ b/core/src/components/datetime/datetime.tsx @@ -53,6 +53,7 @@ import { getPreviousWeek, getPreviousYear, getStartOfWeek, + removeTimeParts, validateParts, } from './utils/manipulation'; import { @@ -1846,7 +1847,7 @@ export class Datetime implements ComponentInterface { this.processMinParts(); this.processMaxParts(); - this.defaultParts = getClosestValidDate({ + const defaultParts = getClosestValidDate({ refParts: todayParts, monthValues, dayValues, @@ -1857,6 +1858,18 @@ export class Datetime implements ComponentInterface { maxParts: this.maxParts, }); + /** + * Presentations without a time picker should not carry + * the current time into the default parts. Otherwise, + * confirming a selection without an initial value emits + * a full timestamp (e.g. `2026-09-01T16:19:00` for + * `presentation="date"`) instead of the date-only string + * the presentation describes. + */ + this.defaultParts = ['time', 'date-time', 'time-date'].includes(this.presentation) + ? defaultParts + : removeTimeParts(defaultParts); + this.processValue(this.value); this.emitStyle(); diff --git a/core/src/components/datetime/test/basic/datetime.e2e.ts b/core/src/components/datetime/test/basic/datetime.e2e.ts index 83e9094640d..0ea00951615 100644 --- a/core/src/components/datetime/test/basic/datetime.e2e.ts +++ b/core/src/components/datetime/test/basic/datetime.e2e.ts @@ -791,6 +791,51 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => { await datetime.evaluate((el: HTMLIonDatetimeElement) => (el.value = '2022-01-01')); await expect(ionChange).not.toHaveReceivedEvent(); }); + + test('should emit a date-only value when confirming a selection without an initial value', async ({ page }) => { + await page.setContent( + ` + + `, + config + ); + + await page.locator('.datetime-ready').waitFor(); + + const ionChange = await page.spyOnEvent('ionChange'); + const targetDay = page.locator('.calendar-day[data-day="10"]:not(.calendar-day-adjacent-day)').first(); + + const expectedValue = await targetDay.evaluate( + (el: HTMLElement) => + `${el.dataset.year}-${String(el.dataset.month).padStart(2, '0')}-${String(el.dataset.day).padStart(2, '0')}` + ); + + await targetDay.click(); + + const ev = await ionChange.next(); + expect(ev.detail.value).toBe(expectedValue); + }); + + test('should emit a value with time when confirming a selection without an initial value on date-time', async ({ + page, + }) => { + await page.setContent( + ` + + `, + config + ); + + await page.locator('.datetime-ready').waitFor(); + + const ionChange = await page.spyOnEvent('ionChange'); + const targetDay = page.locator('.calendar-day[data-day="10"]:not(.calendar-day-adjacent-day)').first(); + + await targetDay.click(); + + const ev = await ionChange.next(); + expect(ev.detail.value).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00$/); + }); }); }); diff --git a/core/src/components/datetime/utils/manipulation.ts b/core/src/components/datetime/utils/manipulation.ts index 152f2f62fec..711512266ae 100644 --- a/core/src/components/datetime/utils/manipulation.ts +++ b/core/src/components/datetime/utils/manipulation.ts @@ -48,6 +48,22 @@ export function convertDataToISO(data: DatetimeParts | DatetimeParts[]): string return rtn; } +/** + * Removes the time fields (hour, minute, ampm) from + * the provided parts so only the date fields remain. + * Used by presentations that do not render a time + * picker, where carrying the current time into the + * default parts would leak a full timestamp into the + * emitted value instead of a date-only string. + */ +export const removeTimeParts = (parts: DatetimeParts): DatetimeParts => { + const dateParts = { ...parts }; + delete dateParts.hour; + delete dateParts.minute; + delete dateParts.ampm; + return dateParts; +}; + /** * Converts an 12 hour value to 24 hours. */