-
Notifications
You must be signed in to change notification settings - Fork 0
/
itsago.min.js
1 lines (1 loc) · 1.58 KB
/
itsago.min.js
1
"use strict";let defaultMixin=function(value,interval){let phrases={seconds:"just now",minute:"about a minute ago",minutes:"%d minutes ago",hour:"an hour ago",hours:"%d hours ago",day:"one day ago",days:"%d days ago",month:"one month ago",months:"%d months ago",year:"one year ago",years:"%d years ago"};let str=phrases[interval];str=str.replace(/%d/i,value);return(str)};let itsago=function(el,datetime,mixin,sticky){if(!mixin){mixin=defaultMixin}else if(typeof mixin!=="function"){let warnObject={target:el};console.warn("[itsago] mixin passed is not a function. Type:",typeof mixin);console.warn(warnObject);mixin=defaultMixin} let intervals={seconds:"seconds",minute:"minute",minutes:"minutes",hour:"hour",hours:"hours",day:"day",days:"days",month:"month",months:"months",year:"year",years:"years"};let time=new Date();if(!datetime){if(el){let elementTime=el.getAttribute("datetime");if(elementTime){time=new Date(elementTime)}}}else{time=new Date(datetime)} let declaredTime=time.getTime();let now=new Date();let timeDifference=now-parseInt(declaredTime);let seconds=Math.floor(timeDifference/1000);let amounts={year:seconds/31536000,month:seconds/2592000,day:seconds/86400,hour:seconds/3600,minute:seconds/60};let timeSince=intervals.seconds;let value=1;for(let i in amounts){value=Math.floor(amounts[i]);if(value>1){let interval=i;timeSince=intervals[i+"s"];break}else if(value===1){let interval=i;timeSince=intervals[i];break}} timeSince=mixin(value,timeSince);let result=timeSince.trim();if(el){el.innerText=result;if(sticky){el.setAttribute("datetime",time.toISOString())} return result}else{return result}}