Make your timestamps easier to read for your users. Very easy to use.
npm install time-difference-simplifier
After the package installation is successful, you can import the package to your project.
// Using import method
import { getTimeDifference } from "time-difference-simplifier";
// Using require method
const { getTimeDifference } = require("time-difference-simplifier")
After the package is imported, you can use the function directly to your code. Here are some example of usage:
const now = new Date();
const timeDifference1 = getTimeDifference(now);
// Result: Just now
const past = new Date(2024, 8, 19, 0, 0, 0));
const timeDifference2 = getTimeDifference(past);
// Result: 1 Hour ago
const future = new Date(2030, 8, 19, 0, 0, 0));
const timeDifference3 = getTimeDifference(future);
// Result: Invalid date
If your date is in the future, the code will return "Invalid date".
Here are all the available result pattern that you can get:
- Just now (less than or equal to 10 seconds)
- x second ago / x seconds ago
- x minute ago / x minutes ago
- x hour ago / x hours ago
- x day ago / x days ago
- x week ago / x weeks ago
- x month ago / x months ago
- x year ago / x years ago
Michael Andreas Prasetio, Indonesia
michaelandreasprasetio@gmail.com