The Angular DatePipe is a built-in pipe module that allows you to format dates in your Angular templates. It provides a convenient way to transform dates into different formats according to locale rules

Syntax

{{ value_expression | date[:format] [:timezone] [:locale] }}

We have to import the “Common Module” library in angular modules. If we use the angular 16+ version and we use standalone components then we also have to import this “Common Module” to utilize this inbuilt Date Pipe.
In syntax we have to pass pipe and date to use datepipe. We can also pass 3 arguments in the date pipe.

Arguments:

  • 1st argument is format which is predefined else custom format we can use. 2nd argument.
  • The 2nd argument is timezone. (e.g., ‘+0430’ or ‘GMT+0430’)
  • The 3rd argument is locale. Used for date language translation like french for fr

Parameter

value_expression –
This is the date object or a number (milliseconds since UTC epoch) that you want to format.

format –
It’s optional & you can specify the date-time format. If not provided, it uses the medium-date format (‘MMM d, y, h:mm:ss a’).

timezone –
Specifies the time zone & its optional. It can be a string representing a time zone offset (e.g., ‘+0430’ or ‘GMT+0430’) or a time zone name from the IANA time zone database (e.g., ‘Asia/Kolkata’).

locale –
It is optional parameter in date pipe & specifies the locale code for the formatting rules. Defaults to the current locale of the application. E.g. (‘en-US’)

Pre-defined Formats

Option Format Output
short M/d/yy, h:mm a 6/15/15, 9:03 AM
medium MMM d, y, h:mm:ss a Jun 15, 2015, 9:03:01 AM
long MMMM d, y, h:mm:ss a z June 15, 2015 at 9:03:01 AM GMT+1
full EEEE, MMMM d, y, h:mm:ss a zzzz Monday, June 15, 2015 at 9:03:01 AM GMT+01:00
shortDate M/d/yy 6/15/15
mediumDate MMM d, y Jun 15, 2015
longDate MMMM d, y June 15, 2015
fullDate EEEE, MMMM d, y Monday, June 15, 2015
shortTime h:mm a 9:03 AM
mediumTime h:mm:ss a 9:03:01 AM
longTime h:mm:ss a z 9:03:01 AM GMT+1
fullTime h:mm:ss a zzzz 9:03:01 AM GMT+01:00

Custom Formats

Field type Format Description Example Value
Era G, GG & GGG Abbreviated AD
GGGG Wide Anno Domini
GGGGG Narrow A
Year y Numeric: minimum digits 2, 20, 201, 2017, 20173
yy Numeric: 2 digits + zero padded 02, 20, 01, 17, 73
yyy Numeric: 3 digits + zero padded 002, 020, 201, 2017, 20173
yyyy Numeric: 4 digits or more + zero padded 0002, 0020, 0201, 2017, 20173
ISO Week-numbering year Y Numeric: minimum digits 2, 20, 201, 2017, 20173
YY Numeric: 2 digits + zero padded 02, 20, 01, 17, 73
YYY Numeric: 3 digits + zero padded 002, 020, 201, 2017, 20173
YYYY Numeric: 4 digits or more + zero padded 0002, 0020, 0201, 2017, 20173
Month M Numeric: 1 digit 9, 12
MM Numeric: 2 digits + zero padded 09, 12
MMM Abbreviated Sep
MMMM Wide September
MMMMM Narrow S
Month standalone L Numeric: 1 digit 9, 12
LL Numeric: 2 digits + zero padded 09, 12
LLL Abbreviated Sep
LLLL Wide September
LLLLL Narrow S
ISO Week of year w Numeric: minimum digits 1… 53
ww Numeric: 2 digits + zero padded 01… 53
Week of month W Numeric: 1 digit 1… 5
Day of month d Numeric: minimum digits 1
dd Numeric: 2 digits + zero padded 01
Week day E, EE & EEE Abbreviated Tue
EEEE Wide Tuesday
EEEEE Narrow T
EEEEEE Short Tu
Week day standalone c, cc Numeric: 1 digit 2
ccc Abbreviated Tue
cccc Wide Tuesday
ccccc Narrow T
cccccc Short Tu
Period a, aa & aaa Abbreviated am/pm or AM/PM
aaaa Wide (fallback to a when missing) ante meridiem/post meridiem
aaaaa Narrow a/p
Period* B, BB & BBB Abbreviated mid.
BBBB Wide am, pm, midnight, noon, morning, afternoon, evening, night
BBBBB Narrow md
Period standalone* b, bb & bbb Abbreviated mid.
bbbb Wide am, pm, midnight, noon, morning, afternoon, evening, night
bbbbb Narrow md
Hour 1-12 h Numeric: minimum digits 1, 12
hh Numeric: 2 digits + zero padded 01, 12
Hour 0-23 H Numeric: minimum digits 0, 23
HH Numeric: 2 digits + zero padded 00, 23
Minute m Numeric: minimum digits 8, 59
mm Numeric: 2 digits + zero padded 08, 59
Second s Numeric: minimum digits 0… 59
ss Numeric: 2 digits + zero padded 00… 59
Fractional seconds S Numeric: 1 digit 0… 9
SS Numeric: 2 digits + zero padded 00… 99
SSS Numeric: 3 digits + zero padded (= milliseconds) 000… 999
Zone z, zz & zzz Short specific non location format (fallback to O) GMT-8
zzzz Long specific non location format (fallback to OOOO) GMT-08:00
Z, ZZ & ZZZ ISO8601 basic format -0800
ZZZZ Long localized GMT format GMT-8:00
ZZZZZ ISO8601 extended format + Z indicator for offset 0 (= XXXXX) -08:00
O, OO & OOO Short localized GMT format GMT-8
OOOO Long localized GMT format GMT-08:00

Examples

1) Basic Example

<!-- Assuming myDate is a Date object in your component -->
<!-- component.html file —->

{{ myDate | date }}

Output (depending on locale and date) default locale will be us: Jul 25, 2024

2) Specifying a Format


{{ myDate | date: ‘short’ }}

Output (depending on locale and date) default locale will be us

7/25/24, 12:00 AM

Available formats include: ‘short’, ‘medium’, ‘long’, ‘full’, or a custom format like ‘dd/MM/yyyy’. Above having all the available formats and custom format you can use it as per your requirement.

3) Using Time Zone



<!-- component.html file —->
<!-- London time zone GMT+1 —->
<!-- myDate = 25-July-2024 09:58 PM —-→

{{ myDate | date: ‘yyyy-MMM-dd HH:mm:ss A’ : ‘+0100’ }}

Output will be 2024/July/25, 05:27:30 PM as per london timezone

4) Using Custom Format & Time Zone


{{ myDate | date: ‘yyyy-MMM-dd HH:mm:ss A’ : '+0100' }}

5) Locale Specific


{{ myDate | date: 'medium' : undefined : 'fr' }}

Output will be 25 juil. 2024 à 00:00:00 as per french locale.

The above pipe there is 3 argos we have passed medium for date format. Undefined is for passing undefined timezone & last is for locale

Key Points

  • Locale: Determines the language and formatting rules used. It affects month and day names, symbols (like AM/PM), and formatting conventions.
  • Time Zone: Specifies how the date and time should be adjusted based on different time zones.
  • Format: Allows customization of how the date and time are displayed. Angular provides several predefined formats, and you can also specify a custom format using predefined format strings.

Considerations

  • Performance: DatePipe relies on Angular’s internationalization (i18n) API, which means it’s affected by Angular’s locale data and performance considerations.
  • Localization: Ensure that the locales and formats used are appropriate for your target audience to ensure a good user experience.

Summary

In summary, the Angular DatePipe is a powerful tool for formatting dates in Angular templates, providing flexibility through different formats, time zones, and locales to suit various application requirements.

Support On Demand!

Angular