A JavaScript library to calculate and compare elapsed time.
- Automatically detects elapsed time: seconds, minutes, hours, months, or years ago.
- Compare two times and get the elapsed time object.
- Customizable and flexible.
- And more to explore!
Via CDN:
<script src="https://cdn.jsdelivr.net/gh/rezzvy/tickago@latest/dist/tickago.min.js"></script>Via npm:
npm install tickagoThen import it:
const TickAgo = require("tickago");Returns a human-readable string relative to the current time.
// From Date object
console.log(TickAgo.moment(new Date("2011-11-11")));
// → "13 years ago"
// From timestamp
console.log(TickAgo.moment(Date.now() - 5000));
// → "5 seconds ago"
// From string
console.log(TickAgo.moment("2011-11-11"));
// → "13 years ago"
// With custom labels (example: Indonesian)
console.log(
TickAgo.moment(Date.now() - 5000, {
past: "{value} {unit}{plural} yang lalu",
future: "dalam {value} {unit}{plural}",
now: "baru saja",
units: {
second: "detik",
},
plural: (v) => (v > 1 ? "" : ""), // no plural 's'
})
);
// → "5 detik yang lalu"timestampcan be a Date object, timestamp number, or string parsable bynew Date().- Invalid values will throw an
Error. - The second parameter (
labels) is optional and must be an object with a structure like the default:
{
past: "{value} {unit}{plural} ago",
future: "in {value} {unit}{plural}",
now: "just now",
units: {
year: "year",
month: "month",
day: "day",
hour: "hour",
minute: "minute",
second: "second"
},
plural: (value, unit) => (value > 1 ? "s" : "")
}The template must contain {value}, {unit}, and {plural}.
If not, it will safely fall back to the default template.
Returns the exact difference between two dates.
console.log(TickAgo.compare("2015-01-01", "2025-01-01"));Output:
{
"years": 10,
"months": 0,
"days": 0,
"hours": 0,
"minutes": 0,
"seconds": 0,
"raw": {
"milliseconds": 315619200000,
"seconds": 315619200,
"minutes": 5260320,
"hours": 87672,
"days": 3653
},
"isFuture": true
}date1anddate2can be Date objects, timestamps, or strings.- Invalid values will throw an
Error. - The
isFutureflag indicates whetherdate2is greater thandate1.
TickAgo will throw descriptive errors in cases such as:
moment()receives an invalid timestamp.compare()receives an invalid date.labelsis not an object or has the wrong structure (e.g.,pluralis not a function).
There's always room for improvement. Feel free to contribute!
The library is licensed under MIT License. Check the license file for more details.