Angular 4 display current time

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

    Actually, you don't need any library for this simple task. If you are doing in your angular project with angular 6+ then import formatDate from the common package and pass other data. Here is a sample:

    import { Component } from '@angular/core';
    import {formatDate } from '@angular/common';
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      today= new Date();
      todaysDataTime = '';
    
      constructor() {
        this.todaysDataTime = formatDate(this.today, 'dd-MM-yyyy hh:mm:ss a', 'en-US', '+0530');
      }
    }
    

    Here is a stackblitz link, you can edit here: https://stackblitz.com/edit/angular-h63y8e

提交回复
热议问题