7Guyo 7Guyo - 2 years ago 152
AngularJS Question

Angular 2, Ionic 2

Am trying to display seconds in real-time.

This is what I have in the component.

//In the component
today: number = Date.now();
//the template
{{ today | date:'medium' }}

This displays the date and time, but the seconds can only change on refresh of the app.

I want to see change of seconds in real-time. Can someone please give a guide.

Answer Source

you can use setInterval to refresh today, remember to clear the created interval in ngOnDestory. But be careful not using it too much for good performance.

private dateInterval: any;

constructor() {}

// add interval at OnInit
ngOnInit() {
  this.dateInterval = setInterval(() => {
    this.today = Date.now();
  }, 1000)

// destory interval at OnDestory
ngOnDestory() {

Plunker demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download