DatePipe
pipe
npm Package | @angular/common |
---|---|
Module | import { DatePipe } from '@angular/common'; |
Source | common/src/pipes/date_pipe.ts |
NgModule | CommonModule |
How To Use
date_expression | date[:format[:timezone[:locale]]]
Description
Where:
-
expression
is a date object or a number (milliseconds since UTC epoch) or an ISO string (https://www.w3.org/TR/NOTE-datetime). -
format
indicates which date/time components to include. The format can be predefined as shown below (all examples are given foren-US
) or custom as shown in the table.-
'short'
: equivalent to'M/d/yy, h:mm a'
(e.g.6/15/15, 9:03 AM
) -
'medium'
: equivalent to'MMM d, y, h:mm:ss a'
(e.g.Jun 15, 2015, 9:03:01 AM
) -
'long'
: equivalent to'MMMM d, y, h:mm:ss a z'
(e.g.June 15, 2015 at 9:03:01 AM GMT+1
) -
'full'
: equivalent to'EEEE, MMMM d, y, h:mm:ss a zzzz'
(e.g.Monday, June 15, 2015 at 9:03:01 AM GMT+01:00
) -
'shortDate'
: equivalent to'M/d/yy'
(e.g.6/15/15
) -
'mediumDate'
: equivalent to'MMM d, y'
(e.g.Jun 15, 2015
) -
'longDate'
: equivalent to'MMMM d, y'
(e.g.June 15, 2015
) -
'fullDate'
: equivalent to'EEEE, MMMM d, y'
(e.g.Monday, June 15, 2015
) -
'shortTime'
: equivalent to'h:mm a'
(e.g.9:03 AM
) -
'mediumTime'
: equivalent to'h:mm:ss a'
(e.g.9:03:01 AM
) -
'longTime'
: equivalent to'h:mm:ss a z'
(e.g.9:03:01 AM GMT+1
) -
'fullTime'
: equivalent to'h:mm:ss a zzzz'
(e.g.9:03:01 AM GMT+01:00
)
-
-
timezone
to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example,'+0430'
(4 hours, 30 minutes east of the Greenwich meridian) If not specified, the local system timezone of the end-user's browser will be used. -
locale
is astring
defining the locale to use (uses the currentLOCALE_ID
by default)
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 | |
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 | |
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 | |
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 |
When the expression is a ISO string without time (e.g. 2016-09-19) the time zone offset is not applied and the formatted text will have the same day, month and year of the expression.
WARNINGS:
- this pipe has only access to en-US locale data by default. If you want to localize the dates in another language, you will have to import data for other locales. See the "I18n guide" to know how to import additional locale data.
- Fields suffixed with * are only available in the extra dataset. See the "I18n guide" to know how to import extra locale data.
- this pipe is marked as pure hence it will not be re-evaluated when the input is mutated. Instead users should treat the date as an immutable object and change the reference when the pipe needs to re-run (this is to avoid reformatting the date on every change detection run which would be an expensive operation).
Examples
Assuming dateObj
is (year: 2015, month: 6, day: 15, hour: 21, minute: 43, second: 11) in the local time and locale is 'en-US':
@Component({ selector: 'date-pipe', template: `<div> <!--output 'Jun 15, 2015'--> <p>Today is {{today | date}}</p> <!--output 'Monday, June 15, 2015'--> <p>Or if you prefer, {{today | date:'fullDate'}}</p> <!--output '9:43 AM'--> <p>The time is {{today | date:'shortTime'}}</p> <!--output 'Monday, June 15, 2015 at 9:03:01 AM GMT+01:00' --> <p>The full date/time is {{today | date:'full'}}</p> <!--output 'Lundi 15 Juin 2015 à 09:03:01 GMT+01:00'--> <p>The full date/time in french is: {{today | date:'full':'':'fr'}}</p> <!--output '2015-06-15 05:03 PM GMT+9'--> <p>The custom date is {{today | date:'yyyy-MM-dd HH:mm a z':'+0900'}}</p> <!--output '2015-06-15 09:03 AM GMT+9'--> <p>The custom date with fixed timezone is {{fixedTimezone | date:'yyyy-MM-dd HH:mm a z':'+0900'}}</p> </div>` }) export class DatePipeComponent { today = Date.now(); fixedTimezone = '2015-06-15T09:03:01+0900'; }
© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v5.angular.io/api/common/DatePipe