Yogesh Chauhan's Blog

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);
}

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to Use password_hash and password_verify to Secure Your User’s Data (Especially Passwords)?PHPMake sure your links have descriptive textUI/UXKilling A Project Part 4: What Would Be Your Top 3 Priorities For Planning The Cancellation?MiscellaneousHow to merge arrays in JavaScript?JavaScriptSocial Media Colors: Sass VariablesMiscellaneousHow to change style of all direct children of an element using jQuery?jQueryHow destructuring works in React?ReactHow to select multiple values in React dropdown?ReactFor In Loop in Swift for BeginnersSwiftHow to get previous days or next days in PHP?PHPHow to add Google Map in WordPress using ACF?WordPressSanitize inputs using built-in WordPress functionsWordPress