@raona/sp
Version:
Raona utilities to work with Sharepoint using pnp/sp
31 lines (30 loc) • 1.43 kB
JavaScript
;
// the argument 'date' will be a string date like:
Object.defineProperty(exports, "__esModule", { value: true });
// "Dec 25, 1995"
// "Mon, 25 Dec 1995 13:30:00 GMT"
// "Mon, 25 Dec 1995 13:30:00 GMT+0430"
// "2019-07-02 15:56:51Z"
// etc...
// see more in https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Date/parse
function getTimeAgo(date) {
var diffFromNow = function (ts) { return Math.floor((Date.parse(ts) - +Date.now()) / 1000 / 60 / 60); };
var diff = -1 * diffFromNow(date); // multiply by -1 for transform on positive the sign of diff variable
var diffDays = Math.ceil(diff / 24);
var diffWeeks = Math.ceil(diffDays / 7);
var diffMonths = Math.ceil(diffDays / 30);
var diffYears = Math.ceil(diffMonths / 12);
var timeAgo;
if (diff < 24)
timeAgo = diff + " " + (diff > 1 ? 'hours ago' : 'hour');
if (24 <= diff && diffDays < 7)
timeAgo = diffDays + " " + (diffDays > 1 ? 'days ago' : 'day');
if (7 <= diffDays && diffDays < 30)
timeAgo = diffWeeks + " " + (diffWeeks > 1 ? 'weeks ago' : 'week');
if (30 <= diffDays && diffMonths < 12)
timeAgo = diffMonths + " " + (diffMonths > 1 ? 'months ago' : 'month');
if (12 <= diffMonths)
timeAgo = diffYears + " " + (diffYears > 1 ? 'years ago' : 'year');
return timeAgo;
}
exports.getTimeAgo = getTimeAgo;