UNPKG

js-time-diff

Version:

a lightweight time difference javascript library

2 lines (1 loc) 859 B
'use strict';var TimeDiff=function(a,b){var c=Math.round;a=new Date(a),b=b?new Date(b):new Date;var d=a.getTime(),e=b.getTime();if(!d)return new Error('Invalid start date');if(!e)return new Error('Invalid end date');var f=e-d,g=0<f?'ago':'after';f=0<f?f:-f;var h=f/1e3,i=h/60,j=i/60,k=j/24,l=k/30;if(12<=l){var n=c(l/12);return 1===n?n+' year '+g:n+' years '+g}if(30<=k){var o=c(l);return 1===o?o+' month '+g:o+' months '+g}if(24<=j){var p=c(k);return 1===p?p+' day '+g:p+' days '+g}if(60<=i){var q=c(j);return 1===q?q+' hour '+g:q+' hours '+g}if(60<=h){var r=c(i);return 1===r?r+' minute '+g:r+' minutes '+g}var m=c(h);return 1===m?m+' second '+g:m+' seconds '+g};(function(){'function'==typeof define&&define.amd?define('TimeDiff',function(){return TimeDiff}):'undefined'!=typeof module&&module.exports?module.exports=TimeDiff:window.TimeDiff=TimeDiff})();