Date Pipe In Angular

Date Pipe In Angular

In this article, we will learn how to use Date Pipe in angular with examples. We can format dates in Angular using the specified format, time zone, and local information using the Angular Date Pipe. It comes with pre-defined formats pre-installed. By establishing new format strings, we can additionally alter the date format. We can change the time zone, the country locale, and so on.

Syntax of Date Pipe

The pipe operator, i.e. |, is used by the Date. In the left side of the |, type the date expression you want to format. On the right, type in the date, then the arguments. It takes three parameters: format, timezone, and locale.

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

Example

Here's an example of a date pipe in its most basic form.
<h3>Using Date Pipe </h3>
<p>Unformatted date : {{toDate }} </p>     //Without pipe
<p>Formatted date : {{toDate | date}} </p>   //With Date Pipe

Date Expression

Anything that evaluates to date can be used as a Date Expression. It could be a Date object, a number (milliseconds since UTC epoch), or an ISO string, for example.

Add the following code in to the app.component.ts file:
toDate: Date = new Date();
numDate=1590319189931;
strDate="Sun May 24 2020 19:16:23";

Add the following code in to the app.component.html file:
<h3>Date Expression </h3>
<p>Date Object : {{toDate | date}} </p>       //May 29, 2022
<p>Number Date : {{numDate | date}} </p>      //May 29, 2022
<p>ISO Date : {{strDate | date}} </p>         //May 29, 2022

Parameters to Date Pipe

Anything that evaluates to date can be used as a Date Expression. It could be a Date object, a number (milliseconds since UTC epoch), or an ISO string, for example.


ParameterData TypeParticulars
formatstringThere are two types of format.
1. predefined formats
2. Custom format string

Default: mediumDate.
timezonestringUse
  • A timezone offset (such as '+05:30')
  • Standard UTC/GMT
  • continental timezone abbreviation.
Default: End-users local system timezone
localestringA locale code for the locale format rules to use.
Default: The value of LOCALE_ID ( which is en-US)
How to Change LOCALE_ID

Date Format

There are two types of formats to choose from.
  1. Pre defined Format
  2. Custom Format string

Pre defined formats

The predefined formats that you can use are listed below. Next to it, we've listed the appropriate custom formats.


FormatEquivalent Custom FormatExampleResult
short'M/d/yy, h:mm a'{{toDate | date:'short'}}5/29/22, 3:40 PM
medium'MMM d, y, h:mm:ss a'{{toDate | date:'medium'}}May 29, 2022, 3:42:17 PM
long'MMMM d, y, h:mm:ss a z'{{toDate | date:'long'}}May 29, 2022 at 3:42:17 PM GMT+5
full'EEEE, MMMM d, y, h:mm:ss a zzzz'{{toDate | date:'full'}}Sunday, May 29, 2022 at 3:42:17 PM GMT+05:30
shortDate'M/d/yy'{{toDate | date:'shortDate'}}5/29/22
mediumDate'MMM d, y'{{toDate | date:'mediumDate'}}May 29, 2022
longDate'MMMM d, y'{{toDate | date:'longDate'}}May 29, 2022
fullDate'EEEE, MMMM d, y'{{toDate | date:'fullDate'}}Sunday, May 29, 2022
shortTime'h:mm a'{{toDate | date:'shortTime'}}3:42 PM
mediumTime'h:mm:ss a'{{toDate | date:'mediumTime'}}3:42:17 PM
longTime'h:mm:ss a z'{{toDate | date:'longTime'}}3:42:17 PM GMT+5
fullTime'h:mm:ss a zzzz'{{toDate | date:'fullTime'}}3:42:17 PM GMT+05:30


Custom Format string

The following is a complete list of available custom formats.

Field typeFormatDescriptionExample Value
EraG, GG & GGGAbbreviatedAD
GGGGWideAnno Domini
GGGGGNarrowA
YearyNumeric: minimum digits2, 20, 201, 2017, 20173
yyumeric: 2 digits + zero padded02, 20, 01, 17, 73
yyyNumeric: 3 digits + zero padded002, 020, 201, 2017, 20173
yyyyNumeric: 4 digits or more + zero padded0002, 0020, 0201, 2017, 20173
MonthMNumeric: 1 digit9, 12
MMNumeric: 2 digits + zero padded09, 12
MMMAbbreviatedSep
MMMMWideSeptember
MMMMMNarrowS
Month standaloneLNumeric: 1 digit9, 12
LLNumeric: 2 digits + zero padded09, 12
LLLAbbreviatedSep
LLLLWideSeptember
LLLLLNarrowS
Week of yearwNumeric: minimum digits1... 53
wwNumeric: 2 digits + zero padded01... 53
Week of monthWNumeric: 1 digit1... 5
Day of monthdNumeric: minimum digits1
ddNumeric: 2 digits + zero padded01
Week dayE, EE & EEEAbbreviatedTue
EEEEWideTuesday
EEEEENarrowT
EEEEEEShortTu
Perioda, aa & aaaAbbreviatedam/pm or AM/PM
aaaaWide (fallback to a when missing)ante meridiem/post meridiem
aaaaaNarrowa/p
Period*B, BB & BBBAbbreviatedmid
BBBBWideam, pm, midnight, noon, morning, afternoon, evening, night
BBBBBNarrowmd.
Period standalone*b, bb & bbbAbbreviatedmid.
bbbbWideam, pm, midnight, noon, morning, afternoon, evening, night
bbbbbNarrowmd
Hour 1-12hNumeric: minimum digits1, 12
hhNumeric: 2 digits + zero padded01, 12
Hour 0-23HNumeric: minimum digits0, 23
HHNumeric: 2 digits + zero padded00, 23
MinutemNumeric: minimum digits8, 59
mmNumeric: 2 digits + zero padded08, 59
SecondsNumeric: minimum digits0... 59
ssNumeric: 2 digits + zero padded00... 59
Fractional secondsSNumeric: 1 digit0... 9
SSNumeric: 2 digits + zero padded00... 99
SSSNumeric: 3 digits + zero padded (= milliseconds)000... 999
Zonez, zz & zzzShort specific non location format (fallback to O)GMT-8
zzzzLong specific non location format (fallback to OOOO)GMT-08:00
Z, ZZ & ZZZISO8601 basic format-0800
ZZZZLong localized GMT formatGMT-8:00
ZZZZZISO8601 extended format + Z indicator for offset 0 (= XXXXX)-08:00
O, OO & OOOShort localized GMT formatGMT-8
OOOOLong localized GMT formatGMT-08:00


Custom Format example

{{toDate | date:'dd/MM/y'}}                //24/05/2020
{{toDate | date:'dd/MM/yy HH:mm'}}         //May 24, 2020, 7:17:26 PM

Timezone Example

The examples below demonstrate how to use time zones.
Date in India (IST Time Zone)  : {{toDate | date:'short':'IST'}}     //Date in India (IST Time Zone) : 5/29/22, 7:32 PM
Date  in USA (CDT Time Zone)   : {{toDate | date:'short':'CDT'}}    //Date in USA (CDT Time Zone) : 5/29/22, 9:02 AM
Date in India (+0530)     : {{toDate | date:'short':'+0530'}}     //Date in India (+0530) : 5/29/22, 7:32 PM
Date in USA (-0700)     : {{toDate | date:'short':'-0500'}}    //Date in USA (-0700) : 5/29/22, 9:02 AM

Locale Date Example

The third argument is the Country Locale.
British date time is {{toDate | date:'dd/MM/yy HH:mm':'GMT':'en-GB'}}       //British date time is 29/05/22 14:26

Conclusion

In this article, we learned how to use Date Pipe in angular with examples; also we can format dates in Angular using the specified format, time zone, and local information using the Angular Date Pipe.

I hope this article helps you and you will like it.👍

If you have any doubt or confusion then free to ask in comment section.

Post a Comment

Previous Post Next Post