This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Rendering an Observable Date with the Async and Date Pipes

1:05 Angular 2 lesson by

Instead of simply pushing numbers on a timer into the template, now we'll move on to pushing actual Dates. We'll still use the Async pipe, but we'll also add on the Date pipe with some formatting to display the Date just the way we want it.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Instead of simply pushing numbers on a timer into the template, now we'll move on to pushing actual Dates. We'll still use the Async pipe, but we'll also add on the Date pipe with some formatting to display the Date just the way we want it.

Avatar
John Christopher Jones

The date pipe won't work in the current release of Safari as of 2016 June 25. Angular2 will self-immolate and very helpfully tell you that the problem arose in [object Object]. If you're trying to get this to work, you'll need to add the Intl polyfill to your index.html.

  1. Install the Intl package.
install --save intl
  1. Then, for English locale, in your index.html under the other "polyfills" you must already have, add:
<script src="node_modules/intl/dist/Intl.js"></script>
<script src="node_modules/intl/locale-data/jsonp/en.js"></script>

To turn this clock into a working clock instead of just pumping out zero through infinity, what we need to do is push out a date. We're going to map, which takes a function. Each time this ticks we're going to return a new date. Don't forget, every time you use a method you have to import it here. I'll go ahead and import map. Now once I hit Save and I refresh over here, you can see that now we'll get some dates printed out.

We actually have control over this date if we use the date pipe, which I'll go ahead and use. Date and then that takes a format string, which I'm just going to go ahead and copy and paste in. Looks like this. I'll hit Save and Refresh. You can see we get the full Wednesday and then a bit nicer format for our time.

All of these formats are the standard date formats you see in other languages. You can find them in the Angular docs if you want to format this however you want.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?