MCPcopy Index your code
hub / github.com/angular/angular / assertValidDateFormat

Function assertValidDateFormat

packages/common/src/i18n/format_date.ts:152–168  ·  view source on GitHub ↗

* Asserts that the given date format is free from common mistakes. Throws an * error if one is found (except for the case of all "Y", in which case we just * log a warning). This should only be called in development mode.

(parts: string[])

Source from the content-addressed store, hash-verified

150 * log a warning). This should only be called in development mode.
151 */
152function assertValidDateFormat(parts: string[]) {
153 if (parts.some((part) => /^Y+$/.test(part)) && !parts.some((part) => /^w+$/.test(part))) {
154 // "Y" indicates "week-based year", which differs from the actual calendar
155 // year for a few days around Jan 1 most years. Unless "w" is also
156 // present (e.g. a date like "2024-W52") this is likely a mistake. Users
157 // probably meant "y" instead.
158 const message = `Suspicious use of week-based year "Y" in date pattern "${parts.join(
159 '',
160 )}". Did you mean to use calendar year "y" instead?`;
161 if (parts.length === 1) {
162 // NOTE: allow "YYYY" with just a warning, since it's used in tests.
163 console.error(formatRuntimeError(RuntimeErrorCode.SUSPICIOUS_DATE_FORMAT, message));
164 } else {
165 throw new RuntimeError(RuntimeErrorCode.SUSPICIOUS_DATE_FORMAT, message);
166 }
167 }
168}
169
170/**
171 * Create a new Date object with the given date value, and the time set to midnight.

Callers 1

formatDateFunction · 0.85

Calls 5

formatRuntimeErrorFunction · 0.85
someMethod · 0.80
joinMethod · 0.65
errorMethod · 0.65
testMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…