typescript
27 lines · 7 steps
Human-readable relative times with Intl
Convert a timestamp into phrases like "3 days ago" by walking through escalating time units.
Explained by
highlit
1const DIVISIONS: { amount: number; unit: Intl.RelativeTimeFormatUnit }[] = [
2 { amount: 60, unit: "seconds" },
3 { amount: 60, unit: "minutes" },
4 { amount: 24, unit: "hours" },
5 { amount: 7, unit: "days" },
6 { amount: 4.34524, unit: "weeks" },
7 { amount: 12, unit: "months" },
8 { amount: Number.POSITIVE_INFINITY, unit: "years" },
9];
10
11export function formatRelativeTime(
12 date: Date | number | string,
13 locale = "en-US",
14): string {
15 const formatter = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
16 const target = date instanceof Date ? date : new Date(date);
17 let duration = (target.getTime() - Date.now()) / 1000;
18
19 for (const { amount, unit } of DIVISIONS) {
20 if (Math.abs(duration) < amount) {
21 return formatter.format(Math.round(duration), unit);
22 }
23 duration /= amount;
24 }
25
26 return formatter.format(Math.round(duration), "years");
27}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A table of unit thresholds turns repetitive time math into a single clean loop.
- 2Intl.RelativeTimeFormat handles localization and natural phrasing so you never hardcode strings.
- 3Dividing the running duration by each unit's size lets one pass scale from seconds up to years.
Related explainers
typescript
import { CallHandler, ExecutionContext, Injectable,
Wrapping responses in a NestJS interceptor
interceptors
rxjs
response-shaping
Intermediate
7 steps
php
<?php namespace App\Support;
Locale-aware formatting with PHP's intl extension
internationalization
encapsulation
constructor-injection
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
import { Pipe, PipeTransform, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core'; @Pipe({ name: 'timeAgo',
A self-refreshing timeAgo pipe in Angular
impure-pipe
change-detection
timers
Advanced
10 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
php
<?php namespace App\Rules;
How a custom phone validation rule works in Laravel
validation
custom-rules
dependency
Intermediate
6 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/human-readable-relative-times-with-intl-explained-typescript-b5db/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.