@bangbu/ts-utils
Version:
Utility functions and constants for Cyberk
22 lines • 746 B
JavaScript
import ms from "ms";
export const timeAgo = (timestamp, { withAgo, } = {}) => {
if (!timestamp)
return "Never";
const diff = Date.now() - new Date(timestamp).getTime();
if (diff < 1000) {
// less than 1 second
return "Just now";
}
else if (diff > 82800000) {
// more than 23 hours – similar to how Twitter displays timestamps
return new Date(timestamp).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: new Date(timestamp).getFullYear() !== new Date().getFullYear()
? "numeric"
: undefined,
});
}
return `${ms(diff)}${withAgo ? " ago" : ""}`;
};
//# sourceMappingURL=time-ago.js.map