MCPcopy Create free account
hub / github.com/angular/angular / formatDate

Function formatDate

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

Source from the content-addressed store, hash-verified

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