Reputation: 829
I want to format a timer in Angular, so bellow 10, 9 becomes 09, 8 - 08, etc. till 0 - 00.
I didn't find a built-in pipe. How can I achieve this? Thanks.
Upvotes: 1
Views: 905
Reputation: 3862
You can use Angular Date Pipe
like this:
<div>{{ today | date : 'EEEE, MMMM d, hh:mm:ss a' }}</div>
TS:
today: Date = new Date(2020,3,5,10,9,8);
Output:
Sunday, April 5, 10:09:08 AM
here hh, mm, ss
will make sure you have two digits even when value is less than 10
Upvotes: 0
Reputation: 323
Use the following pipes as follows
{{ value_expression | date [ : format [ : timezone [ : locale ] ] ] }}
{{datevar | date: 'short'}}
Upvotes: 0
Reputation: 528
Here is a simple version, showing the time in seconds:
Component:
private timer;
private counter: Date;
ngOnInit() {
this.timer = Observable.timer(0,1000)
.subscribe(t => {
this.counter = new Date(0,0,0,0,0,0);
this.counter.setSeconds(t);
});
}
ngOnDestroy(){
this.timer.unsubscribe();
}
Template:
<div class="client-time">
<span>Client time</span><br/>
<strong>{{counter | date:'HH:mm:ss'}} seconds</strong>
</div>
Upvotes: 0
Reputation: 304
import built-in function
formatDate
import { formatDate } from '@angular/common';
12-Hours format
this.currentDateTime = formatDate(responseData.CreatedDateTime, 'MMM, dd yyyy hh:mm:ss aa', 'en-US');
24-Hours format
this.currentDateTime = formatDate(responseData.CreatedDateTime, 'MMM, dd yyyy HH:mm:ss', 'en-US');
Upvotes: 1