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);
}
Angular 9 date time timestamp update variables