A lightweight, zero-dependency TypeScript library for formatting timestamps into human-readable "time ago" strings.
- 🚀 Zero dependencies - Lightweight and fast
- 🔧 TypeScript first - Full type safety out of the box
- 🎯 Simple API - Just one function, multiple input types
- ⚡ Performance focused - Optimized for speed
- 🌍 Future-ready - Optional future date handling
- 📝 Well tested - Comprehensive test coverage
npm install timeago-format
yarn add timeago-format
pnpm add timeago-format
import { timeAgo } from 'timeago-format';
// Basic usage
console.log(timeAgo(new Date(Date.now() - 30000))); // "30s ago"
console.log(timeAgo(new Date(Date.now() - 300000))); // "5m ago"
console.log(timeAgo(new Date(Date.now() - 3600000))); // "1h ago"
// Works with different input types
console.log(timeAgo(Date.now() - 30000)); // timestamp
console.log(timeAgo('2024-01-15T10:30:00Z')); // ISO string
console.log(timeAgo(new Date('2024-01-15'))); // Date object
This is the default and recommended approach:
import { timeAgo } from 'timeago-format';
If you want to use require()
syntax, ensure your project is set up for CommonJS:
-
Make sure your
package.json
doesn't have"type": "module"
(or set it to"type": "commonjs"
) -
Use
.js
file extensions (not.mjs
) - Use require syntax:
const { timeAgo } = require('timeago-format');
// Usage is exactly the same
console.log(timeAgo(new Date(Date.now() - 30000))); // "30s ago"
Create a new project with CommonJS support:
# Create project directory
mkdir my-timeago-project
cd my-timeago-project
# Initialize package.json (ensure no "type": "module")
npm init -y
# Install the package
npm install timeago-format
# Create a test file (test.js)
echo 'const { timeAgo } = require("timeago-format");
console.log(timeAgo(new Date(Date.now() - 30000)));' > test.js
# Run the test
node test.js
Formats a date/timestamp into a human-readable "time ago" string.
-
input
(Date | string | number
) - The date to format -
options
(TimeAgoOptions
) - Optional configuration
string
- Formatted time string
interface TimeAgoOptions {
referenceDate?: Date; // Custom reference date (default: now)
includeFuture?: boolean; // Handle future dates with "in X" format
}
Time Range | Format | Example |
---|---|---|
< 1 second | Just now |
Just now |
1-59 seconds | Ns ago |
30s ago |
1-59 minutes | Nm ago |
5m ago |
1-23 hours | Nh ago |
3h ago |
24+ hours (same year) | Month Day |
May 30 |
24+ hours (different year) | Month Day, Year |
Dec 12, 2024 |
import { timeAgo } from 'timeago-format';
const now = new Date();
// Just now
timeAgo(new Date(now.getTime() - 500)); // "Just now"
// Seconds
timeAgo(new Date(now.getTime() - 30000)); // "30s ago"
// Minutes
timeAgo(new Date(now.getTime() - 300000)); // "5m ago"
// Hours
timeAgo(new Date(now.getTime() - 7200000)); // "2h ago"
// Same year dates
timeAgo(new Date('2025-03-15')); // "Mar 15" (if current year is 2025)
// Different year dates
timeAgo(new Date('2024-12-25')); // "Dec 25, 2024"
timeAgo(new Date('2026-07-04')); // "Jul 4, 2026"
const customRef = new Date('2025-06-01T12:00:00Z');
timeAgo(new Date('2025-06-01T11:30:00Z'), {
referenceDate: customRef
}); // "30m ago"
const future = new Date(Date.now() + 3600000); // +1 hour
// Default behavior - shows as date
timeAgo(future); // "Jun 1" or "Jun 1, 2025"
// With future handling enabled
timeAgo(future, { includeFuture: true }); // "in 1h"
// Date object
timeAgo(new Date('2024-01-15T10:30:00Z'));
// Timestamp (number)
timeAgo(1705314600000);
// ISO string
timeAgo('2024-01-15T10:30:00.000Z');
// Date string
timeAgo('Jan 15, 2024');
try {
timeAgo('invalid-date');
} catch (error) {
console.error(error.message); // "Invalid date input: invalid-date"
}
// Type-safe with TypeScript
timeAgo(null); // ❌ TypeScript error
timeAgo(undefined); // ❌ TypeScript error
Full TypeScript support with exported types:
import { timeAgo, DateInput, TimeAgoOptions } from 'timeago-format';
const input: DateInput = new Date();
const options: TimeAgoOptions = {
referenceDate: new Date(),
includeFuture: true
};
const result: string = timeAgo(input, options);
// CommonJS (Node.js)
const { timeAgo } = require('timeago-format');
// ES Modules (Modern JavaScript/TypeScript)
import { timeAgo } from 'timeago-format';
// Default export
import timeAgo from 'timeago-format';
// Dynamic import (ES2020+)
const { timeAgo } = await import('timeago-format');
- Social media feeds - Show post timestamps
- Chat applications - Message time indicators
- Activity logs - Track user actions
- Comment systems - Display comment ages
- File managers - Show file modification times
- Analytics dashboards - Time-based data visualization
- Zero dependencies - No external libs to slow you down
- Lightweight - < 2KB minified + gzipped
- Fast calculations - Optimized date arithmetic
- Tree-shakable - Import only what you need
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see the LICENSE file for details.
- Simple & focused - Does one thing really well
- TypeScript native - Built with TS from the ground up
- Zero dependencies - No bloat, just functionality
- Well tested - Comprehensive test coverage
- Modern API - Clean, intuitive interface