UNPKG

since-time-ago-typescript

Version:

typescript package to convert time stamp into a readable format

31 lines 1.23 kB
"use strict"; exports.__esModule = true; var since = function (timestamp) { var second = 1000; var minute = 60 * 1000; var hour = 60 * minute; var day = 24 * hour; var month = 30 * day; var year = 12 * month; if (!timestamp) return undefined; if (isNaN(new Date(timestamp).getTime())) { throw new Error("Invalid timestamp passed to 'since()'"); } var difference = new Date().getTime() - new Date(timestamp).getTime(); if (Math.floor(difference / year) > 1) return Math.floor(difference / year) + " years ago"; if (Math.floor(difference / month) > 1) return Math.floor(difference / month) + " months ago"; if (Math.floor(difference / day) > 1) return Math.floor(difference / day) + " days ago"; if (Math.floor(difference / hour) > 1) return Math.floor(difference / hour) + " hours ago"; if (Math.floor(difference / minute) > 1) return Math.floor(difference / minute) + " minutes ago"; if (Math.floor(difference / second) > 1) return Math.floor(difference / second) + " seconds ago"; return "just now"; }; exports["default"] = since; //# sourceMappingURL=index.js.map