Angular 4 display current time

前端 未结 6 1613
青春惊慌失措
青春惊慌失措 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:16

    today: number = Date.now();
    
    constructor() {
        setInterval(() => {this.today = Date.now()}, 1);
    }
    

    If you want to format this on html page, use it like this:

    {{today | date:'fullDate'}} {{today | date:'h:mm:ss a'}}

    I'm adding one more point, which I was finding and could come closer to my solution because of this answer, so for another one who's seeking a similar thing, I'm adding this too :)

    If you want to get [Year: Month :date :hours: minutes with am/pm ] format with all numbers (ex : 12 instead of the string "December"), simply use the following

    {{today | date:'y:M:d:h:mm a'}}

提交回复
热议问题