typescript
52 lines · 10 steps
A self-refreshing timeAgo pipe in Angular
An impure Angular pipe that renders relative timestamps and reschedules its own change detection so the label stays current.
Explained by
highlit
1import { Pipe, PipeTransform, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core';
2
3@Pipe({
4 name: 'timeAgo',
5 standalone: true,
6 pure: false,
7})
8export class TimeAgoPipe implements PipeTransform, OnDestroy {
9 private timer: ReturnType<typeof setTimeout> | null = null;
10
11 constructor(private cdr: ChangeDetectorRef, private ngZone: NgZone) {}
12
13 transform(value: Date | string | number): string {
14 this.clearTimer();
15 const then = new Date(value).getTime();
16 const seconds = Math.round((Date.now() - then) / 1000);
17
18 this.scheduleUpdate(seconds);
19 return this.format(seconds);
20 }
21
22 private format(seconds: number): string {
23 if (seconds < 5) return 'just now';
24 if (seconds < 60) return `${seconds} seconds ago`;
25 const minutes = Math.round(seconds / 60);
26 if (minutes < 60) return `${minutes} minute${minutes === 1 ? '' : 's'} ago`;
27 const hours = Math.round(minutes / 60);
28 if (hours < 24) return `${hours} hour${hours === 1 ? '' : 's'} ago`;
29 const days = Math.round(hours / 24);
30 return `${days} day${days === 1 ? '' : 's'} ago`;
31 }
32
33 private scheduleUpdate(seconds: number): void {
34 const delay = seconds < 60 ? 1000 : seconds < 3600 ? 30000 : 300000;
35 this.ngZone.runOutsideAngular(() => {
36 this.timer = setTimeout(() => {
37 this.ngZone.run(() => this.cdr.markForCheck());
38 }, delay);
39 });
40 }
41
42 private clearTimer(): void {
43 if (this.timer) {
44 clearTimeout(this.timer);
45 this.timer = null;
46 }
47 }
48
49 ngOnDestroy(): void {
50 this.clearTimer();
51 }
52}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1An impure pipe runs on every change detection cycle, so it can manage live, time-dependent output.
- 2Scheduling timers outside Angular's zone avoids triggering needless change detection until you explicitly re-enter it.
- 3Pipes that own resources like timers must clean them up in ngOnDestroy to prevent leaks.
Related explainers
typescript
import { CallHandler, ExecutionContext, Injectable,
Wrapping responses in a NestJS interceptor
interceptors
rxjs
response-shaping
Intermediate
7 steps
typescript
type RetryOptions = { retries?: number; timeoutMs?: number; baseDelayMs?: number;
Retry with timeout and backoff in TypeScript
promises
retry
exponential-backoff
Intermediate
10 steps
typescript
const DIVISIONS: { amount: number; unit: Intl.RelativeTimeFormatUnit }[] = [ { amount: 60, unit: "seconds" }, { amount: 60, unit: "minutes" }, { amount: 24, unit: "hours" },
Human-readable relative times with Intl
internationalization
date-formatting
lookup-table
Intermediate
7 steps
typescript
import { Component } from '@angular/core'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { HttpClient } from '@angular/common/http'; import { AsyncPipe } from '@angular/common';
Reactive type-ahead search in Angular
rxjs
reactive-forms
debounce
Intermediate
9 steps
typescript
function throttle<T extends (...args: any[]) => void>( fn: T, limit: number ): (...args: Parameters<T>) => void {
Building a trailing-edge throttle in TypeScript
throttling
closures
generics
Intermediate
7 steps
typescript
interface TokenBucketOptions { capacity: number; refillPerSecond: number; }
How a token bucket rate limiter works
rate-limiting
token-bucket
lazy-evaluation
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-self-refreshing-timeago-pipe-in-angular-explained-typescript-0410/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.