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: Yesterday
Support 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)
The Timeago clock is useful for displaying relative times, such as "2 days ago" or "A few moments ago".
The concise
format displays:
Just now
when within a minute- The time when within the last day
Yesterday
when within the last two days- The day of the week when within the last week
- The date when beyond a week
NOTE: If
daysOfWeek
is enabled, the day of the week, month and day when beyond a week NOTE: Iflongform
is enabled, the month, day, and year when beyond a week
The verbose
format displays:
A few moments ago
when within a minuteX minutes ago
when within an hourX hours ago
when within a dayX days ago
when within a weekX weeks ago
when within a monthX months ago
when within a yearX years ago
when beyond two years
The verbose extended
with only words for times less than 5 format displays:
A few moments ago
when within a minuteA couple of minutes ago
when within 2 minutesA few minutes ago
when within 5 minutesX minutes ago
when within an hourAn hour ago
when within 2 hoursA few hours ago
when within 5 hoursX hours ago
when within a dayA day ago
when within 2 daysA few days ago
when within 5 daysX days ago
when within a weekA week ago
when within 2 weeksA few weeks ago
when within 5 weeksX weeks ago
when within a monthA month ago
when within 2 monthsA few months ago
when within 5 monthsX months ago
when within a yearA year ago
when within 2 yearsA few years ago
when within 5 yearsX years ago
when beyond 5 years
The date to convert to a readable time string.
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.
The locale to use for formatting. Defaults to en-US
.
The options to use for formatting.
Whether to use the verbose format for the timeago clock. Defaults to false
.
Whether to convert the timeago clock to words, eg. A few minutes ago
. Works for both concise & verbose formats. Defaults to true
.
Whether to include the ago
suffix for the timeago clock. Defaults to true
.
Whether to include Today
for times within the past day for the timeago clock. Defaults to true
.
Whether to include Just now
for times within the past minute for the timeago clock. Defaults to true
.
Whether to include the day of the week for the timeago clock. Defaults to false
.
Whether to use the longform date format for the timeago clock. Defaults to false
.
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
.
The number of characters to abbreviate the days of the week to. Defaults to 0
(no abbreviation).
The number of characters to abbreviate the months to. Defaults to 0
(no abbreviation).
The character to use for abbreviating days & months. Defaults to .
.
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 PM