@bangbu/ts-utils
Version:
Utility functions and constants for Cyberk
29 lines • 1.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeAgo = void 0;
const ms_1 = __importDefault(require("ms"));
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 `${(0, ms_1.default)(diff)}${withAgo ? " ago" : ""}`;
};
exports.timeAgo = timeAgo;
//# sourceMappingURL=time-ago.js.map