time-ago-pipe
Version:
A really simple, lightweight Angular pipe for converting a date string into a time ago
3 lines (2 loc) • 1.64 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core")):"function"==typeof define&&define.amd?define(["exports","@angular/core"],t):t(e["time-ago-pipe"]={},e.ng.core)}(this,function(e,t){"use strict";var n=function(){function e(e,t){this.changeDetectorRef=e,this.ngZone=t}return e.prototype.transform=function(e){var t=this;this.removeTimer();var n=new Date(e),o=new Date,r=Math.round(Math.abs((o.getTime()-n.getTime())/1e3)),a=Number.isNaN(r)?1e3:1e3*this.getSecondsUntilUpdate(r);this.timer=this.ngZone.runOutsideAngular(function(){return"undefined"!=typeof window?window.setTimeout(function(){t.ngZone.run(function(){return t.changeDetectorRef.markForCheck()})},a):null});var i=Math.round(Math.abs(r/60)),u=Math.round(Math.abs(i/60)),s=Math.round(Math.abs(u/24)),h=Math.round(Math.abs(s/30.416)),c=Math.round(Math.abs(s/365));return Number.isNaN(r)?"":r<=45?"a few seconds ago":r<=90?"a minute ago":i<=45?i+" minutes ago":i<=90?"an hour ago":u<=22?u+" hours ago":u<=36?"a day ago":s<=25?s+" days ago":s<=45?"a month ago":s<=345?h+" months ago":s<=545?"a year ago":c+" years ago"},e.prototype.ngOnDestroy=function(){this.removeTimer()},e.prototype.removeTimer=function(){this.timer&&(window.clearTimeout(this.timer),this.timer=null)},e.prototype.getSecondsUntilUpdate=function(e){return e<60?2:e<3600?30:e<86400?300:3600},e}();n.decorators=[{type:t.Pipe,args:[{name:"timeAgo",pure:!1}]}],n.ctorParameters=function(){return[{type:t.ChangeDetectorRef},{type:t.NgZone}]},e.TimeAgoPipe=n,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=time-ago-pipe.umd.min.js.map