Skip to content

Commit 982dc85

Browse files
authored
fix(datetime): warn when parsing an invalid date value (#25049)
1 parent 8318659 commit 982dc85

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

core/src/components/datetime/datetime.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ComponentInterface, EventEmitter } from '@stencil/core';
22
import { Component, Element, Event, Host, Method, Prop, State, Watch, h, writeTask } from '@stencil/core';
3+
import { printIonWarning } from '@utils/logging';
34
import { caretDownSharp, caretUpSharp, chevronBack, chevronDown, chevronForward } from 'ionicons/icons';
45

56
import { getIonMode } from '../../global/ionic-global';
@@ -298,15 +299,20 @@ export class Datetime implements ComponentInterface {
298299
* This allows us to update the current value's date/time display without
299300
* refocusing or shifting the user's display (leaves the user in place).
300301
*/
301-
const { month, day, year, hour, minute } = parseDate(this.value);
302-
this.activePartsClone = {
303-
...this.activeParts,
304-
month,
305-
day,
306-
year,
307-
hour,
308-
minute,
309-
};
302+
const valueDateParts = parseDate(this.value);
303+
if (valueDateParts) {
304+
const { month, day, year, hour, minute } = valueDateParts;
305+
this.activePartsClone = {
306+
...this.activeParts,
307+
month,
308+
day,
309+
year,
310+
hour,
311+
minute,
312+
};
313+
} else {
314+
printIonWarning(`Unable to parse date string: ${this.value}. Please provide a valid ISO 8601 datetime string.`);
315+
}
310316
}
311317

312318
this.emitStyle();

core/src/utils/logging/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Logs a warning to the console with an Ionic prefix
3+
* to indicate the library that is warning the developer.
4+
*
5+
* @param message - The string message to be logged to the console.
6+
*/
7+
export const printIonWarning = (message: string) => {
8+
return console.warn(`[Ionic Warning]: ${message}`);
9+
};

0 commit comments

Comments
 (0)