Angular 4 display current time

前端 未结 6 1723
青春惊慌失措
青春惊慌失措 2021-02-02 11:44

What is the correct (canonical) way to display current time in angular 4 change detection system?

The problem is as follows: current time changes constantly, each mome

6条回答
  •  眼角桃花
    2021-02-02 12:12

    As per Angular documentation on the DatePipe Link, Date.now() can be used.

    import { Component } from '@angular/core';
    @Component({
        selector: 'my-app',
        template: `{{ now | date:'HH:mm:ss'}}`
    })
    export class AppComponent {
        now:number;
    
        constructor() {
            setInterval(() => {
              this.now = Date.now();
            }, 1);
        }
    }
    

提交回复
热议问题