Search code examples
angulartypescriptangular11current-time

How to show current time that should change every second or minute in angular 11


I am working on an angular 11 project. I want to display current time and date and the time should change for every minute.

I tried with new Date(). But it shows only the time when the program run time.

This is what I tried. But it does not works for me

.ts file

  today= Date.now();
  UKDataTime = '';

  this.UKDataTime = formatDate(this.today, 'dd-MM-yyyy hh:mm:ss a', 'en-US', '+0100');

.html file

 <p class="current-time">UK: {{UKDataTime}}</p> 

What can i do display live time in angular 11?


Solution

  • You need to change date value every second for example you can use setInterval for this

    setInterval(() => {
        this.UKDataTime = formatDate(Date.now(), 'dd-MM-yyyy hh:mm:ss a', 'en-US', '+0100');
    }, 1000)