datetime-relative Edge Function — Utilities
UtilitiesFormat a date as a relative time string like '2 hours ago' or 'in 3 days' using Intl.RelativeTimeFormat.
Edge function datetime-relative Format a date as a relative time string like '2 hours ago' or 'in 3 days' using Intl.RelativeTimeFormat.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.
npx aerostack add navin/datetime-relative Use with AI Assistants
MCPConnect Claude, Cursor, or any MCP-compatible client — then call this function by slug
① Add MCP Server
Add this once — access all Aerostack functions from your AI tool.
{
"mcpServers": {
"aerostack": {
"url": "https://mcp.aerostack.dev",
"type": "http"
}
}
} ② Call this function
Ask your AI to use the call_function tool with this slug:
call_function({
slug: "datetime-relative",
args: {
"date": null,
"options": null
}
}) datetime-relative — Format dates as relative time strings
Generate human-friendly relative time strings like "2 hours ago" or "in 3 days" using native Intl.RelativeTimeFormat.
API
POST /api/datetime-relative
Request body
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
date |
string | number |
✅ | — | Date to format |
options.now |
string | number |
❌ | Date.now() |
Reference time |
options.locale |
string |
❌ | 'en' |
BCP47 locale |
options.style |
string |
❌ | 'long' |
long, short, or narrow |
Success response (200)
{
"success": true,
"data": {
"relative": "2 hours ago",
"diffMs": -7200000,
"direction": "past"
}
}
Usage
Direct import
import { datetimeRelative } from '@aerostack/functions/datetime-relative';
const result = datetimeRelative({ date: post.createdAt });
console.log(result.relative); // "posted 3 days ago"
// French locale
const fr = datetimeRelative({ date: event.startsAt, options: { locale: 'fr' } });
console.log(fr.relative); // "dans 2 jours"
Use Cases
- Showing "posted N minutes ago" on social feeds and comment sections
- Displaying "expires in 3 days" on trial or subscription banners
- Internationalised timestamps in multi-language applications
Metadata
Tags
Publisher
@navin verified
Build and publish your own functions
Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.
More Utilities Functions
Browse Utilities Functions →datetime-add
by @navin
Add or subtract a duration from a date, supporting units from milliseconds to years with correct month-end handling.
datetime-business-hours
by @navin
Check if a datetime falls within business hours and return the next opening time.
datetime-diff
by @navin
Calculate the difference between two dates in specified units (ms, seconds, minutes, hours, days, weeks, months, years).
datetime-duration-format
by @navin
Format a duration in milliseconds to a human-readable string with long, short, or compact styles.
datetime-format
by @navin
Format a date to a string using format tokens like YYYY-MM-DD HH:mm:ss with optional timezone support.
datetime-parse
by @navin
Parse a date string or unix timestamp to a normalised Unix millisecond timestamp and ISO string.
Frequently asked questions
What does the datetime-relative function do? +
datetime-relative is a serverless edge function for utilities automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.
How do I deploy the datetime-relative function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/datetime-relative ``` It will be live on Cloudflare Workers in seconds.
What runtime does datetime-relative use? +
datetime-relative runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the datetime-relative function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.