rago
Version:
A utility helper to calculate "time ago" or "from now " in a readable format using Intl API
2 lines (1 loc) • 696 B
JavaScript
var rago=function(t){"use strict";const e=[["second",1],["minute",60],["hour",3600],["day",86400],["month",2592e3],["year",31104e3]];function n(t){const n=Math.round((t.valueOf()-Date.now())/1e3);for(let r=1;r<e.length;r+=1)if(Math.abs(n)<e[r][1]){const[t,o]=e[r-1];return{unit:t,delta:Math.round(n/o)}}return{delta:Math.round(n/31104e3),unit:"year"}}return t.diffUnitFor=n,t.fromNow=function(t,e="en"){let r=t;if(!t)return"";if(!(t instanceof Date)&&(r=new Date(t),isNaN(r.getTime())))return"";const o=new Intl.RelativeTimeFormat(e,{style:"long",numeric:"auto"}),{delta:a,unit:u}=n(r);return o.format(a,u)},Object.defineProperty(t,"__esModule",{value:!0}),t[Symbol.toStringTag]="Module",t}({});