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 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

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
PHP Login System using PDO Part 1: Create User Registration PagePHPWhat is HTMLUnknownElement?HTMLThe SELECT DISTINCT Statement in SQLSQL/MySQLAND, OR and NOT boolean operators in Envision BasicEnvision BasicThe Types of DatabasesMiscellaneousWordPress get_posts ExamplesWordPressHow to get the first element with a class name xyz using JavaScript?JavaScriptSQL Basics: What does the asterisk (*) mean in a query?SQL/MySQLHow to create a horizontal scrolling menu with and without flex using CSS (or SCSS)?CSSVariables and Distance Functions in SCSS (Sass)SCSSSequence generator (range) using JavaScript Array.from()JavaScriptIN and BETWEEN Operators in SQLSQL/MySQL