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 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 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What is a “promise” in JavaScript?JavaScriptHow to get front page or home page ID in WordPress?WordPressCheck if mixin is being called in a style rule or not in SCSSSCSSHow to zoom an element on hover using CSS?CSSHow to create a cross-browser smooth scrolling with jQuery?jQueryDefault Values in SCSS (Sass)SCSSSanitize inputs using built-in WordPress functionsWordPressHTML Copyright SymbolHTMLDebugging in WordPress Part 3: SCRIPT_DEBUG and SAVEQUERIESWordPressHow to CREATE TABLE in SQL with and without using Another Table?SQL/MySQLCrypto.getRandomValues() method in JavaScriptJavaScriptIntroduction to Angular modules Part 3: NgModules vs JavaScript modules and Angular librariesAngular