UNPKG

@carbon/utilities

Version:

Utilities and helpers to drive consistency across software products using the Carbon Design System

2 lines (1 loc) 639 B
function i(a,o){const t=new Intl.RelativeTimeFormat(o?.locale,{style:o?.style??"long"}),f=typeof a=="number"?new Date(a):a,m=Date.now(),l=Math.floor((m-f.getTime())/1e3),r=Math.floor(l/60),n=Math.floor(r/60),e=Math.floor(n/24),s=Math.floor(e/7),c=Math.floor(s/4),h=Math.floor(e/365);return Math.abs(l)<60?new Intl.RelativeTimeFormat(o?.locale,{numeric:"auto",style:o?.style??"long"}).format(0,"seconds"):Math.abs(r)<60?t.format(r*-1,"minutes"):Math.abs(n)<24?t.format(n*-1,"hours"):Math.abs(e)<7?t.format(e*-1,"days"):Math.abs(s)<4?t.format(s*-1,"weeks"):Math.abs(e)<365?t.format(c*-1,"months"):t.format(h*-1,"years")}export{i as format};