Sub topic 5
Formatting dates
This chapter covers Angular's DatePipe. It assumes you know basic TypeScript, template interpolation, and how a standalone component lists its dependencies in imports. You should also know the pipe operator | and how parameters follow a colon.
What DatePipe accepts
DatePipe formats a value as a date string for display. The component keeps the raw value, and the template decides how it looks.
The input can be one of these:
- A JavaScript
Dateobject. - A number of milliseconds since the Unix epoch.
- An ISO 8601 string such as
'2024-01-15T10:30:00Z'.
Any other string, or an invalid Date, makes the pipe throw an error at render time. The pipe also accepts null and undefined and returns null for them, so a value that has not loaded yet renders as empty text.
DatePipe is exported from @angular/common. In a standalone component you can use it in a template by adding it to imports.
import { Component } from '@angular/core';
import { DatePipe } from '@angular/common';
@Component({
selector: 'app-created',
standalone: true,
imports: [DatePipe],
template: `
<p>{{ createdAt | date }}</p>
<p>{{ createdAt | date:'mediumDate' }}</p>
<p>{{ createdAt | date:'yyyy-MM-dd HH:mm' }}</p>
`,
})
export class CreatedComponent {
createdAt = new Date(2024, 0, 15, 10, 30);
}With the default en-US locale, the three lines render as Jan 15, 2024, Jan 15, 2024 and 2024-01-15 10:30. With no format argument, the pipe uses 'mediumDate'.
Predefined formats
The first parameter is a format. It can be a predefined name or a custom pattern. Predefined names follow the active locale, so they are the safest choice when the app may be shown in several regions.
| Name | Example output (en-US) |
|---|---|
| `short` | `1/15/24, 10:30 AM` |
| `medium` | `Jan 15, 2024, 10:30:00 AM` |
| `long` | `January 15, 2024 at 10:30:00 AM GMT+1` |
| `shortDate` | `1/15/24` |
| `mediumDate` | `Jan 15, 2024` |
| `longDate` | `January 15, 2024` |
| `fullDate` | `Monday, January 15, 2024` |
| `shortTime` | `10:30 AM` |
| `mediumTime` | `10:30:00 AM` |
Custom patterns
A custom pattern is a string of letters. Each letter group stands for one part of the date, and repeating a letter changes its width.
| Letters | Meaning | Example |
|---|---|---|
| `yyyy` | Four-digit year | `2024` |
| `MM` | Month, two digits | `01` |
| `MMM` | Month, short name | `Jan` |
| `MMMM` | Month, full name | `January` |
| `dd` | Day of month, two digits | `15` |
| `EEEE` | Weekday, full name | `Monday` |
| `HH` | Hour, 24-hour clock | `10` |
| `hh` | Hour, 12-hour clock | `10` |
| `mm` | Minutes | `30` |
| `a` | AM or PM | `AM` |
Text inside single quotes is printed as written. In a template, wrap the whole pattern in the other kind of quote, for example date:"dd 'of' MMMM".
Time zone and the third parameter
By default the pipe formats in the browser's local time zone. The second parameter overrides it. It takes an offset such as '+0200' or 'UTC'.
<p>{{ createdAt | date:'shortTime':'UTC' }}</p>
<p>{{ createdAt | date:'HH:mm':'+0900' }}</p>The third parameter is a locale identifier such as 'de'. It overrides the app-wide locale for this one expression. The locale data must be registered first. A later chapter covers that setup.
<p>{{ createdAt | date:'fullDate':'UTC':'en-US' }}</p>Common errors
- Passing a non-ISO string. A string like
'15/01/2024'is ambiguous, so the pipe cannot parse it. Convert it to aDatein the component, or store ISO strings. - Mixing up
yyyyandYYYY. UppercaseYYYYis the week-based year. Near New Year it can show the wrong year, for example2025for 30 December 2024. Useyyyy. - Mixing up
MMandmm. Uppercase is the month and lowercase is minutes. - Expecting a time zone conversion for date-only values. A string like
'2024-01-15'is read as UTC midnight. In a time zone west of UTC, the local date can appear as 14 January.
How the pieces fit
Exercise
A component holds d = new Date(2024, 0, 15, 14, 5). Write a template expression that displays Monday, 15 January 2024 style output without using a predefined name, then say which pattern letters you used for the weekday and the month. Why would YYYY be risky for the year?
Show answer
{{ d | date:'EEEE, dd MMMM yyyy' }} renders Monday, 15 January 2024. EEEE is the full weekday name and MMMM is the full month name. YYYY is the week-based year, so around the end of December and start of January it can show a different year from the calendar year.
Exercise
A server sends '2024-03-10' and users in New York report seeing March 9. What is the cause, and what is the fix in the template?
Show answer
The string is parsed as UTC midnight, and New York is behind UTC, so the local date is the previous day. Pass the UTC time zone: {{ value | date:'mediumDate':'UTC' }}.