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 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #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
10 Usability Blunders to AvoidUI/UXHow to include CSS and JavaScript files in your WordPress plugin?WordPressRBV Framework and closing of big brandsMiscellaneousHow to insert Bootstrap 4 in Angular 9 app?AngularWhat’s a repository on Github?MiscellaneousHow to link/add CSS file to HTML Document?CSSWhat is the correct way to enqueue multiple CSS files in WordPress?WordPressLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPHow to add Read More Read Less Button using JavaScript?JavaScriptHow to add Local State to a Class in React?ReactHow to add a Pie Chart in Angular App?AngularIs there a difference between SCSS and Sass?SCSS