UNPKG

@alessiofrittoli/date-utils

Version:

Lightweight TypeScript date utility functions library

2 lines (1 loc) 486 B
var o=[{amount:60,unit:"seconds"},{amount:60,unit:"minutes"},{amount:24,unit:"hours"},{amount:7,unit:"days"},{amount:4.345238095238096,unit:"weeks"},{amount:12,unit:"months"},{amount:Number.POSITIVE_INFINITY,unit:"years"}],I=(t=new Date,a,u={numeric:"auto"})=>{t=new Date(t);let m=new Intl.RelativeTimeFormat(a,u),n=(t.getTime()-new Date().getTime())/1e3;for(let e=0;e<o.length;e++){let i=o[e];if(Math.abs(n)<i.amount)return m.format(Math.round(n),i.unit);n/=i.amount}};export{I as a};