MCPcopy Index your code
hub / github.com/ionic-team/ionic-framework / parseDate

Function parseDate

core/src/components/datetime/utils/parse.ts:66–128  ·  view source on GitHub ↗
(val: string | string[] | undefined | null)

Source from the content-addressed store, hash-verified

64export function parseDate(val: string | string[]): DatetimeParts | DatetimeParts[] | undefined;
65export function parseDate(val: string | string[] | undefined | null): DatetimeParts | DatetimeParts[] | undefined;
66export function parseDate(val: string | string[] | undefined | null): DatetimeParts | DatetimeParts[] | undefined {
67 if (Array.isArray(val)) {
68 const parsedArray: DatetimeParts[] = [];
69 for (const valStr of val) {
70 const parsedVal = parseDate(valStr);
71
72 /**
73 * If any of the values weren't parsed correctly, consider
74 * the entire batch incorrect. This simplifies the type
75 * signatures by having "undefined" be a general error case
76 * instead of returning (Datetime | undefined)[], which is
77 * harder for TS to perform type narrowing on.
78 */
79 if (!parsedVal) {
80 return undefined;
81 }
82
83 parsedArray.push(parsedVal);
84 }
85
86 return parsedArray;
87 }
88
89 // manually parse IS0 cuz Date.parse cannot be trusted
90 // ISO 8601 format: 1994-12-15T13:47:20Z
91 let parse: any[] | null = null;
92
93 if (val != null && val !== '') {
94 // try parsing for just time first, HH:MM
95 parse = TIME_REGEXP.exec(val);
96 if (parse) {
97 // adjust the array so it fits nicely with the datetime parse
98 parse.unshift(undefined, undefined);
99 parse[2] = parse[3] = undefined;
100 } else {
101 // try parsing for full ISO datetime
102 parse = ISO_8601_REGEXP.exec(val);
103 }
104 }
105
106 if (parse === null) {
107 // wasn't able to parse the ISO datetime
108 printIonWarning(
109 `[ion-datetime] - Unable to parse date string: ${val}. Please provide a valid ISO 8601 datetime string.`
110 );
111 return undefined;
112 }
113
114 // ensure all the parse values exist with at least 0
115 for (let i = 1; i < 8; i++) {
116 parse[i] = parse[i] !== undefined ? parseInt(parse[i], 10) : undefined;
117 }
118
119 // can also get second and millisecond from parse[6] and parse[7] if needed
120 return {
121 year: parse[1],
122 month: parse[2],
123 day: parse[3],

Callers 6

DatetimeButtonClass · 0.90
DatetimeClass · 0.90
componentWillLoadMethod · 0.90
parse.spec.tsFile · 0.90
parseMaxPartsFunction · 0.85
parseMinPartsFunction · 0.85

Calls 2

printIonWarningFunction · 0.90
pushMethod · 0.45

Tested by

no test coverage detected