@trainingmode/readable-time
v1.0.0
Published
A TypeScript utility for converting dates into human-readable time strings with localization support
Maintainers
Readme
Readable Time
Converts a date to a readable time string.
const timeago = toReadableTime({
time,
});
const now = new Date();
console.log({ now, time, timeago });
// now: 2023-11-13T08:12:00.000Z
// time: 2023-11-12T02:22:00.000Z
// timeago: YesterdaySupport for multiple formats:
- 24-hour clock, eg. 23:15
- 12-hour clock, eg. 11:15:00 PM
- 12-hour clock short, eg. 11:15 PM
- 12-hour clock short padded, eg. 01:15 AM
- Timeago clock with multiple formats:
- Concise, eg.
Yesterday - Verbose, eg.
2 days ago - Verbose Extended with only words for times less than 5, eg.
A few minutes ago
- Concise, eg.
Localization is supported with the following locales:
en-US(default)
Timeago Clock
The Timeago clock is useful for displaying relative times, such as "2 days ago" or "A few moments ago".
The concise format displays:
Just nowwhen within a minute- The time when within the last day
Yesterdaywhen within the last two days- The day of the week when within the last week
- The date when beyond a week
NOTE: If
daysOfWeekis enabled, the day of the week, month and day when beyond a week > NOTE: Iflongformis enabled, the month, day, and year when beyond a week
The verbose format displays:
A few moments agowhen within a minuteX minutes agowhen within an hourX hours agowhen within a dayX days agowhen within a weekX weeks agowhen within a monthX months agowhen within a yearX years agowhen beyond two years
The verbose extended with only words for times less than 5 format displays:
A few moments agowhen within a minuteA couple of minutes agowhen within 2 minutesA few minutes agowhen within 5 minutesX minutes agowhen within an hourAn hour agowhen within 2 hoursA few hours agowhen within 5 hoursX hours agowhen within a dayA day agowhen within 2 daysA few days agowhen within 5 daysX days agowhen within a weekA week agowhen within 2 weeksA few weeks agowhen within 5 weeksX weeks agowhen within a monthA month agowhen within 2 monthsA few months agowhen within 5 monthsX months agowhen within a yearA year agowhen within 2 yearsA few years agowhen within 5 yearsX years agowhen beyond 5 years
Parameters
time
The date to convert to a readable time string.
format
The format to convert the date to. Defaults to timeago.
clock-24: 24-hour clock, eg. 23:15clock-long: 12-hour clock, eg. 11:15:00 PMclock-short: 12-hour clock short, eg. 11:15 PMclock-short-pad: 12-hour clock short padded, eg. 01:15 AMtimeago: Timeago clock with multiple formats.
locale
The locale to use for formatting. Defaults to en-US.
options
The options to use for formatting.
verbose
Whether to use the verbose format for the timeago clock. Defaults to false.
convertToWords
Whether to convert the timeago clock to words, eg. A few minutes ago. Works for both concise & verbose formats. Defaults to true.
includeAgoSuffix
Whether to include the ago suffix for the timeago clock. Defaults to true.
includeToday
Whether to include Today for times within the past day for the timeago clock. Defaults to true.
includeJustNow
Whether to include Just now for times within the past minute for the timeago clock. Defaults to true.
daysOfWeek
Whether to include the day of the week for the timeago clock. Defaults to false.
longform
Whether to use the longform date format for the timeago clock. Defaults to false.
longTimeAgoThresholdDays
The threshold in days to use for the timeago clock. If the time is beyond this threshold, the timeago clock will display A long time ago. Defaults to -1.
abbreviateDays
The number of characters to abbreviate the days of the week to. Defaults to 0 (no abbreviation).
abbreviateMonths
The number of characters to abbreviate the months to. Defaults to 0 (no abbreviation).
abbreviatePeriod
The character to use for abbreviating days & months. Defaults to ..
Examples
iMessage Style Timestamps
const createdAt = Date.now();
const day = toReadableTime({
time: createdAt,
options: {
includeToday: true,
includeJustNow: false,
},
});
const time = toReadableTime({
time: createdAt,
format: "clock-short", // 12-hour clock
});
const timeago = `${day} ${time}`;
console.log(timeago);
// Today 11:15 PMContributors
Alfred R. Duarte
Installation
npm install @trainingmode/readable-timeUsage with TypeScript/JavaScript
import { toReadableTime } from "@trainingmode/readable-time";
// Basic usage
const timeago = toReadableTime({
time: new Date(),
});
// With options
const formattedTime = toReadableTime({
time: new Date(),
format: "clock-short",
locale: "en-US",
options: {
verbose: true,
convertToWords: true,
},
});