YogeshChauhan . com

Angular 9 time clock update every minute, second, hour

in Angular on September 16, 2020

Define the variable in your constructor first


today: number = Date.now();

Add setInterval() method in ngOnInit

This will update the time every minute (60 seconds)


ngOnInit(): void {
  setInterval(() => {         
    this.today = Date.now();
    console.log(this.today); 
  }, 60000);
}

Use that today variable in your HTML template using DatePipe


{{today | date:'mediumDate'}}

This will update the time every second


ngOnInit(): void {
  setInterval(() => {         
    this.today = Date.now();
    console.log(this.today); 
  }, 1000);
}

This will update the time every hour


ngOnInit(): void {
  setInterval(() => {         
    this.today = Date.now();
    console.log(this.today); 
  }, 360000);
}
amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How does Binding work in JavaScript?JavaScriptWordPress: How to print ACF array field values?WordPressSelect statement in Postgres with examplesPostgresLearn how to use Self JOIN in SQL and MySQLSQL/MySQLIntroduction to Angular modules Part 2: NgModules and componentsAngularWhat are keys in React?ReactSolution for Xcode 11 Command PhaseScriptExecution failed with a nonzero exit code errorMiscCROSS JOIN in PostgresPostgresWordPress: How to print ACF repeater field values?WordPressHow to change the style for checked radio button or checkboxes using CSS?CSSHow to create flickering text using CSS?CSSSteps to Secure a VPNMisc