Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added round parameter to timeBetween()

  • Loading branch information...
commit 0286baa5499cb799fa08a5ae28981051767e77e2 1 parent 32fee07
dominiklaubach authored
Showing with 15 additions and 9 deletions.
  1. +12 −6 d8.js
  2. +3 −3 d8.min.js
View
18 d8.js
@@ -51,29 +51,35 @@ var D8 = (function() {
return this.date.getTime();
}
- function timeBetween(date, returnType) {
+ function timeBetween(date, returnType, round) {
var firstDateInMilliseconds = this.getTimestamp() ? this.getTimestamp() : null;
var secondDateInMilliseconds = date.getTimestamp() ? date.getTimestamp() : null;
+ round = !(round === false);
if (firstDateInMilliseconds && secondDateInMilliseconds) {
+ var timeBetween;
switch (returnType) {
case 'days':
- return (secondDateInMilliseconds - firstDateInMilliseconds) / (24 * 60 * 60 * 1000);
+ timeBetween = (secondDateInMilliseconds - firstDateInMilliseconds) / (24 * 60 * 60 * 1000);
break;
case 'hours':
- return (secondDateInMilliseconds - firstDateInMilliseconds) / (60 * 60 * 1000);
+ timeBetween = (secondDateInMilliseconds - firstDateInMilliseconds) / (60 * 60 * 1000);
break;
case 'minutes':
- return (secondDateInMilliseconds - firstDateInMilliseconds) / (60 * 1000);
+ timeBetween = (secondDateInMilliseconds - firstDateInMilliseconds) / (60 * 1000);
break;
case 'seconds':
- return (secondDateInMilliseconds - firstDateInMilliseconds) / 1000;
+ timeBetween = (secondDateInMilliseconds - firstDateInMilliseconds) / 1000;
break;
case 'milliseconds':
default:
- return (secondDateInMilliseconds - firstDateInMilliseconds);
+ timeBetween = (secondDateInMilliseconds - firstDateInMilliseconds);
break;
}
+ if(round) {
+ timeBetween = Math.round(timeBetween);
+ }
+ return timeBetween;
} else if (firstDateInMilliseconds) {
throw('Error: Invalid d8 object passed when calling timeBetween()!');
} else {
View
6 d8.min.js
@@ -1,6 +1,6 @@
-var D8=function(){return{date:new Date,now:function(){var a=new D8;a.date=new Date;return a},addDays:function(a){return this.addMilliseconds(a*864E5)},addHours:function(a){return this.addMilliseconds(a*36E5)},addMinutes:function(a){return this.addMilliseconds(a*6E4)},addSeconds:function(a){return this.addMilliseconds(a*1E3)},addMilliseconds:function(a){var b=new D8;b.date=new Date(this.getTimestamp()+a);return b},timeBetween:function(a,b){var c=this.getTimestamp()?this.getTimestamp():null,d=a.getTimestamp()?
-a.getTimestamp():null;if(c&&d)switch(b){case "days":return(d-c)/864E5;case "hours":return(d-c)/36E5;case "minutes":return(d-c)/6E4;case "seconds":return(d-c)/1E3;default:return d-c}else if(c)throw"Error: Invalid d8 object passed when calling timeBetween()!";else throw"Error: Trying to access timeBetween() on an invalid d8 object.";},yesterday:function(){return this.addDays(-1)},tomorrow:function(){return this.addDays(1)},getTimestamp:function(){return this.date.getTime()},getCalendarWeek:function(){year=
-parseInt(this.format("yyyy"));month=parseInt(this.format("m"));day=parseInt(this.format("d"));var a=Math.floor((14-month)/12),b=year+4800-a,a=day+Math.floor((153*(month+12*a-3)+2)/5)+365*b+Math.floor(b/4)-Math.floor(b/100)+Math.floor(b/400)-32045,a=(a+31741-a%7)%146097%36524%1461,b=Math.floor(a/1460);return Math.floor(((a-b)%365+b)/7)+1},format:function(a,b){if(isNaN(this.getTimestamp()))throw"Error: Trying to access format() on an invalid d8 object.";var c=/\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
+var D8=function(){return{date:new Date,now:function(){var a=new D8;a.date=new Date;return a},addDays:function(a){return this.addMilliseconds(a*864E5)},addHours:function(a){return this.addMilliseconds(a*36E5)},addMinutes:function(a){return this.addMilliseconds(a*6E4)},addSeconds:function(a){return this.addMilliseconds(a*1E3)},addMilliseconds:function(a){var b=new D8;b.date=new Date(this.getTimestamp()+a);return b},timeBetween:function(a,b,c){var d=this.getTimestamp()?this.getTimestamp():null,a=a.getTimestamp()?
+a.getTimestamp():null;if(d&&a){switch(b){case "days":b=(a-d)/864E5;break;case "hours":b=(a-d)/36E5;break;case "minutes":b=(a-d)/6E4;break;case "seconds":b=(a-d)/1E3;break;default:b=a-d}c!==!1&&(b=Math.round(b));return b}else if(d)throw"Error: Invalid d8 object passed when calling timeBetween()!";else throw"Error: Trying to access timeBetween() on an invalid d8 object.";},yesterday:function(){return this.addDays(-1)},tomorrow:function(){return this.addDays(1)},getTimestamp:function(){return this.date.getTime()},
+getCalendarWeek:function(){year=parseInt(this.format("yyyy"));month=parseInt(this.format("m"));day=parseInt(this.format("d"));var a=Math.floor((14-month)/12),b=year+4800-a,a=day+Math.floor((153*(month+12*a-3)+2)/5)+365*b+Math.floor(b/4)-Math.floor(b/100)+Math.floor(b/400)-32045,a=(a+31741-a%7)%146097%36524%1461,b=Math.floor(a/1460);return Math.floor(((a-b)%365+b)/7)+1},format:function(a,b){if(isNaN(this.getTimestamp()))throw"Error: Trying to access format() on an invalid d8 object.";var c=/\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
d=/[^-+\dA-Z]/g,f=function(a,b){a=String(a);for(b=b||2;a.length<b;)a="0"+a;return a};if(arguments.length==1&&Object.prototype.toString.call(this.date)=="[object String]"&&!/\d/.test(this.date))a=this.date;var l=["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],m=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","January","February","March","April","May","June","July","August","September","October","November","December"],
e=b?"getUTC":"get",h=this.date[e+"Date"](),j=this.date[e+"Day"](),i=this.date[e+"Month"](),n=this.date[e+"FullYear"](),g=this.date[e+"Hours"](),o=this.date[e+"Minutes"](),p=this.date[e+"Seconds"](),e=this.date[e+"Milliseconds"](),k=b?0:this.date.getTimezoneOffset(),q={d:h,dd:f(h),ddd:l[j],dddd:l[j+7],D:j,m:i+1,mm:f(i+1),mmm:m[i],mmmm:m[i+12],yy:String(n).slice(2),yyyy:n,h:g%12||12,hh:f(g%12||12),H:g,HH:f(g),M:o,MM:f(o),s:p,ss:f(p),l:f(e,3),L:f(e>99?Math.round(e/10):e),t:g<12?"a":"p",tt:g<12?"am":
"pm",T:g<12?"A":"P",TT:g<12?"AM":"PM",Z:b?"UTC":(String(this).match(c)||[""]).pop().replace(d,""),o:(k>0?"-":"+")+f(Math.floor(Math.abs(k)/60)*100+Math.abs(k)%60,4),S:["th","st","nd","rd"][h%10>3?0:(h%100-h%10!=10)*h%10]};return a.replace(/d{1,4}|D{1}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,function(a){return a in q?q[a]:a.slice(1,a.length-1)})},getDatesOfCalendarWeek:function(a){year=this.format("yyyy");for(var b=this.create("01/01/"+year),c=b.format("D"),d=null,d=a?c==1?b:b.addDays(8-
Please sign in to comment.
Something went wrong with that request. Please try again.