MCPcopy
hub / github.com/angular/angular / formatDate

Function formatDate

packages/common/src/i18n/format_date.ts:86–135  ·  view source on GitHub ↗
(
  value: string | number | Date,
  format: string,
  locale: string,
  timezone?: string,
)

Source from the content-addressed store, hash-verified

84 * @publicApi
85 */
86export function formatDate(
87 value: string | number | Date,
88 format: string,
89 locale: string,
90 timezone?: string,
91): string {
92 let date = toDate(value);
93 assertValidDateFormatLength(format);
94 const namedFormat = getNamedFormat(locale, format);
95 format = namedFormat || format;
96
97 let parts: string[] = [];
98 let match;
99 while (format) {
100 match = DATE_FORMATS_SPLIT.exec(format);
101 if (match) {
102 parts = parts.concat(match.slice(1));
103 const part = parts.pop();
104 if (!part) {
105 break;
106 }
107 format = part;
108 } else {
109 parts.push(format);
110 break;
111 }
112 }
113
114 if (typeof ngDevMode === 'undefined' || ngDevMode) {
115 assertValidDateFormat(parts);
116 }
117
118 let dateTimezoneOffset = date.getTimezoneOffset();
119 if (timezone) {
120 dateTimezoneOffset = timezoneToOffset(timezone, dateTimezoneOffset);
121 date = convertTimezoneToLocal(date, timezone, true);
122 }
123
124 let text = '';
125 parts.forEach((value) => {
126 const dateFormatter = getDateFormatter(value);
127 text += dateFormatter
128 ? dateFormatter(date, locale, dateTimezoneOffset)
129 : value === "''"
130 ? "'"
131 : value.replace(/(^'|'$)/g, '').replace(/''/g, "'");
132 });
133
134 return text;
135}
136
137function assertValidDateFormatLength(format: string) {
138 if (format.length > MAX_DATE_FORMAT_LENGTH) {

Callers 3

expectDateFormatAsFunction · 0.90
transformMethod · 0.90

Calls 11

toDateFunction · 0.85
getNamedFormatFunction · 0.85
assertValidDateFormatFunction · 0.85
timezoneToOffsetFunction · 0.85
convertTimezoneToLocalFunction · 0.85
getDateFormatterFunction · 0.85
popMethod · 0.80
pushMethod · 0.45
forEachMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…