4

I have the following code to show the current date:

this.whatTime = Observable.interval(1000).map(x => new Date()).share();

And in my template:

{{whatTime | async}}

My problem is that the date is too long and not formatted as I wish.

enter image description here

All I want to show is: 15/09/16 19:07:11

Any ideas?

TheUnreal
  • 23,434
  • 46
  • 157
  • 277

3 Answers3

5

Use Angular's built-in DatePipe:

{{ whatTime | async | date:'d/M/yy hh:mm:ss' }}

This converts Thu Sep 15 2016 18:15:17 GMT+0200 (Central Europe Daylight Time) into your desired template: 15/9/16 06:15:17.

You can read more about Angular's DatePipe and its formats here and you can read more about Angular's pipes in general here.

Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
1

Use Angular2 built in Date Pipe

{{whatTime | async | date:'yMdjm'}}
Pritesh Acharya
  • 1,596
  • 5
  • 15
  • 36
0

You should use this:

import moment from 'moment';

Then you should call it like that:

moment(timestamp).fromNow();

This will give you the current time.

CuriousDeveloper
  • 77
  • 1
  • 2
  • 12
  • No moment.js tag and there is functionality in Angular (which is tagged) to do the job. – RobG Mar 15 '18 at 03:42