( value: string | number | Date, format: string, locale: string, timezone?: string, )
| 84 | * @publicApi |
| 85 | */ |
| 86 | export 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 | |
| 137 | function assertValidDateFormatLength(format: string) { |
| 138 | if (format.length > MAX_DATE_FORMAT_LENGTH) { |
no test coverage detected
searching dependent graphs…