From be2f093265f374154ff7e8bc0612b9e5d3366da9 Mon Sep 17 00:00:00 2001 From: Alan Knowles Date: Wed, 9 Mar 2022 09:46:47 +0800 Subject: [PATCH] add simple interval support to Date.getElapsed() --- Date.js | 29 ++++++++++++++++++++++++++--- docs/json/roodata.json | 10 ++++++++-- docs/src/Date.js.html | 29 ++++++++++++++++++++++++++--- docs/symbols/Date.json | 10 ++++++++-- roojs-all.js | 21 +++++++++++---------- roojs-core-debug.js | 28 ++++++++++++++++++++++++++-- roojs-core.js | 21 +++++++++++---------- roojs-debug.js | 28 ++++++++++++++++++++++++++-- 8 files changed, 142 insertions(+), 34 deletions(-) diff --git a/Date.js b/Date.js index fffb3b71d1..dab725c026 100644 --- a/Date.js +++ b/Date.js @@ -104,12 +104,35 @@ document.write(dt.format(Date.patterns.ShortDate)); /** Returns the number of milliseconds between this date and date @param {Date} date (optional) Defaults to now - @return {Number} The diff in milliseconds + @param {String} interval (optional) Default Date.MILLI, A valid date interval enum value (eg. Date.DAY) + @return {Number} The diff in milliseconds or units of interval @member Date getElapsed */ -Date.prototype.getElapsed = function(date) { - return Math.abs((date || new Date()).getTime()-this.getTime()); +Date.prototype.getElapsed = function(date, interval) +{ + date = date || new Date(); + var ret = Math.abs(date.getTime()-this.getTime()); + switch (interval) { + + case Date.SECOND: + return Math.floor(ret / (1000)); + case Date.MINUTE: + return Math.floor(ret / (100*60)); + case Date.HOUR: + return Math.floor(ret / (100*60*60)); + case Date.DAY: + return Math.floor(ret / (100*60*60*24)); + case Date.MONTH: // this does not give exact number...?? + return ((date.format("Y") - this.format("Y")) * 12) + (date.format("m") - this.format("m")); + case Date.YEAR: // this does not give exact number...?? + return (date.format("Y") - this.format("Y")); + + case Date.MILLI: + default: + return ret; + } }; + // was in date file.. diff --git a/docs/json/roodata.json b/docs/json/roodata.json index 536ec34bd8..a6e0b65989 100644 --- a/docs/json/roodata.json +++ b/docs/json/roodata.json @@ -331,7 +331,7 @@ "name" : "getElapsed", "type" : "function", "desc" : "Returns the number of milliseconds between this date and date", - "sig" : "(date)", + "sig" : "(date, interval)", "static" : false, "memberOf" : "", "isStatic" : false, @@ -349,13 +349,19 @@ "type" : "Date", "desc" : "(optional) Defaults to now", "isOptional" : false + }, + { + "name" : "interval", + "type" : "String", + "desc" : "(optional) Default Date.MILLI, A valid date interval enum value (eg. Date.DAY)", + "isOptional" : false } ], "returns" : [ { "name" : "", "type" : "Number", - "desc" : "The diff in milliseconds" + "desc" : "The diff in milliseconds or units of interval" } ] }, diff --git a/docs/src/Date.js.html b/docs/src/Date.js.html index 274cd357e2..768075bcbb 100644 --- a/docs/src/Date.js.html +++ b/docs/src/Date.js.html @@ -104,12 +104,35 @@ document.write(dt.format(Date.patterns.ShortDate)); /** Returns the number of milliseconds between this date and date @param {Date} date (optional) Defaults to now - @return {Number} The diff in milliseconds + @param {String} interval (optional) Default Date.MILLI, A valid date interval enum value (eg. Date.DAY) + @return {Number} The diff in milliseconds or units of interval @member Date getElapsed */ -Date.prototype.getElapsed = function(date) { - return Math.abs((date || new Date()).getTime()-this.getTime()); +Date.prototype.getElapsed = function(date, interval) +{ + date = date || new Date(); + var ret = Math.abs(date.getTime()-this.getTime()); + switch (interval) { + + case Date.SECOND: + return Math.floor(ret / (1000)); + case Date.MINUTE: + return Math.floor(ret / (100*60)); + case Date.HOUR: + return Math.floor(ret / (100*60*60)); + case Date.DAY: + return Math.floor(ret / (100*60*60*24)); + case Date.MONTH: // this does not give exact number...?? + return ((date.format("Y") - this.format("Y")) * 12) + (date.format("m") - this.format("m")); + case Date.YEAR: // this does not give exact number...?? + return (date.format("Y") - this.format("Y")); + + case Date.MILLI: + default: + return ret; + } }; + // was in date file.. diff --git a/docs/symbols/Date.json b/docs/symbols/Date.json index a6c9d723c8..88706989d8 100644 --- a/docs/symbols/Date.json +++ b/docs/symbols/Date.json @@ -214,7 +214,7 @@ "name" : "getElapsed", "type" : "function", "desc" : "Returns the number of milliseconds between this date and date", - "sig" : "(date)", + "sig" : "(date, interval)", "static" : false, "memberOf" : "", "isStatic" : false, @@ -232,13 +232,19 @@ "type" : "Date", "desc" : "(optional) Defaults to now", "isOptional" : false + }, + { + "name" : "interval", + "type" : "String", + "desc" : "(optional) Default Date.MILLI, A valid date interval enum value (eg. Date.DAY)", + "isOptional" : false } ], "returns" : [ { "name" : "", "type" : "Number", - "desc" : "The diff in milliseconds" + "desc" : "The diff in milliseconds or units of interval" } ] }, diff --git a/roojs-all.js b/roojs-all.js index 3b9244d4b2..2f9683656a 100644 --- a/roojs-all.js +++ b/roojs-all.js @@ -37,16 +37,17 @@ Roo.applyIf(Array.prototype,{indexOf:function(o){for(var i=0,A=this.length;i 0) {"; +Date.prototype.getElapsed=function(A,B){A=A||new Date();var C=Math.abs(A.getTime()-this.getTime());switch(B){case Date.SECOND:return Math.floor(C/(1000));case Date.MINUTE:return Math.floor(C/(100*60));case Date.HOUR:return Math.floor(C/(100*60*60));case Date.DAY:return Math.floor(C/(100*60*60*24)); +case Date.MONTH:return ((A.format("Y")-this.format("Y"))*12)+(A.format("m")-this.format("m"));case Date.YEAR:return (A.format("Y")-this.format("Y"));case Date.MILLI:default:return C;}};Date.parseFunctions={count:0};Date.parseRegexes=[];Date.formatFunctions={count:0} +;Date.prototype.dateFormat=function(A){if(Date.formatFunctions[A]==null){Date.createNewFormat(A);}var B=Date.formatFunctions[A];return this[B]();};Date.prototype.format=Date.prototype.dateFormat;Date.createNewFormat=function(A){var B="format"+Date.formatFunctions.count++; +Date.formatFunctions[A]=B;var C="Date.prototype."+B+" = function(){return ";var D=false;var ch='';for(var i=0;i 0) {"; var F="";var G=false;var ch='';for(var i=0;i= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"+"{v = new Date(y, m, d, h, i, s); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"+"{v = new Date(y, m, d, h, i); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0 && d > 0 && h >= 0)\n"+"{v = new Date(y, m, d, h); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0 && d > 0)\n"+"{v = new Date(y, m, d); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0)\n"+"{v = new Date(y, m); v.setFullYear(y);}\n"+"else if (y >= 0)\n"+"{v = new Date(y); v.setFullYear(y);}\n"+"}return (v && (z || o))?\n"+" ((z)? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + (z*1)) :\n"+" v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v\n"+";}"; Date.parseRegexes[C]=new RegExp("^"+F+"$");eval(E);};Date.formatCodeToRegex=function(A,B){switch(A){case "D":return {g:0,c:null,s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};case "j":return {g:1,c:"d = parseInt(results["+B+"], 10);\n",s:"(\\d{1,2})"};case "d":return {g:1,c:"d = parseInt(results["+B+"], 10);\n",s:"(\\d{2})"} ;case "l":return {g:0,c:null,s:"(?:"+Date.dayNames.join("|")+")"};case "S":return {g:0,c:null,s:"(?:st|nd|rd|th)"};case "w":return {g:0,c:null,s:"\\d"};case "z":return {g:0,c:null,s:"(?:\\d{1,3})"};case "W":return {g:0,c:null,s:"(?:\\d{2})"};case "F":return {g:1,c:"m = parseInt(Date.monthNumbers[results["+B+"].substring(0, 3)], 10);\n",s:"("+Date.monthNames.join("|")+")"} diff --git a/roojs-core-debug.js b/roojs-core-debug.js index e655f12d25..040021febc 100644 --- a/roojs-core-debug.js +++ b/roojs-core-debug.js @@ -1205,12 +1205,36 @@ document.write(dt.format(Date.patterns.ShortDate)); /** Returns the number of milliseconds between this date and date @param {Date} date (optional) Defaults to now + @param {Date} date (optional) Defaults to now + @param {String} interval A valid date interval enum value (eg. Date.DAY) @return {Number} The diff in milliseconds @member Date getElapsed */ -Date.prototype.getElapsed = function(date) { - return Math.abs((date || new Date()).getTime()-this.getTime()); +Date.prototype.getElapsed = function(date, interval) +{ + date = date || new Date(); + var ret = Math.abs(date.getTime()-this.getTime()); + switch (interval) { + + case Date.SECOND: + return Math.floor(ret / (1000)); + case Date.MINUTE: + return Math.floor(ret / (100*60)); + case Date.HOUR: + return Math.floor(ret / (100*60*60)); + case Date.DAY: + return Math.floor(ret / (100*60*60*24)); + case Date.MONTH: // this does not give exact number...?? + return ((date.format("Y") - this.format("Y")) * 12) + (date.format("m") - this.format("m")); + case Date.YEAR: // this does not give exact number...?? + return (date.format("Y") - this.format("Y")); + + case Date.MILLI: + default: + return ret; + } }; + // was in date file.. diff --git a/roojs-core.js b/roojs-core.js index 051147b5c3..8e77b72b4d 100644 --- a/roojs-core.js +++ b/roojs-core.js @@ -37,16 +37,17 @@ Roo.applyIf(Array.prototype,{indexOf:function(o){for(var i=0,A=this.length;i 0) {"; +Date.prototype.getElapsed=function(A,B){A=A||new Date();var C=Math.abs(A.getTime()-this.getTime());switch(B){case Date.SECOND:return Math.floor(C/(1000));case Date.MINUTE:return Math.floor(C/(100*60));case Date.HOUR:return Math.floor(C/(100*60*60));case Date.DAY:return Math.floor(C/(100*60*60*24)); +case Date.MONTH:return ((A.format("Y")-this.format("Y"))*12)+(A.format("m")-this.format("m"));case Date.YEAR:return (A.format("Y")-this.format("Y"));case Date.MILLI:default:return C;}};Date.parseFunctions={count:0};Date.parseRegexes=[];Date.formatFunctions={count:0} +;Date.prototype.dateFormat=function(A){if(Date.formatFunctions[A]==null){Date.createNewFormat(A);}var B=Date.formatFunctions[A];return this[B]();};Date.prototype.format=Date.prototype.dateFormat;Date.createNewFormat=function(A){var B="format"+Date.formatFunctions.count++; +Date.formatFunctions[A]=B;var C="Date.prototype."+B+" = function(){return ";var D=false;var ch='';for(var i=0;i 0) {"; var F="";var G=false;var ch='';for(var i=0;i= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"+"{v = new Date(y, m, d, h, i, s); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"+"{v = new Date(y, m, d, h, i); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0 && d > 0 && h >= 0)\n"+"{v = new Date(y, m, d, h); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0 && d > 0)\n"+"{v = new Date(y, m, d); v.setFullYear(y);}\n"+"else if (y >= 0 && m >= 0)\n"+"{v = new Date(y, m); v.setFullYear(y);}\n"+"else if (y >= 0)\n"+"{v = new Date(y); v.setFullYear(y);}\n"+"}return (v && (z || o))?\n"+" ((z)? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + (z*1)) :\n"+" v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v\n"+";}"; Date.parseRegexes[C]=new RegExp("^"+F+"$");eval(E);};Date.formatCodeToRegex=function(A,B){switch(A){case "D":return {g:0,c:null,s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};case "j":return {g:1,c:"d = parseInt(results["+B+"], 10);\n",s:"(\\d{1,2})"};case "d":return {g:1,c:"d = parseInt(results["+B+"], 10);\n",s:"(\\d{2})"} ;case "l":return {g:0,c:null,s:"(?:"+Date.dayNames.join("|")+")"};case "S":return {g:0,c:null,s:"(?:st|nd|rd|th)"};case "w":return {g:0,c:null,s:"\\d"};case "z":return {g:0,c:null,s:"(?:\\d{1,3})"};case "W":return {g:0,c:null,s:"(?:\\d{2})"};case "F":return {g:1,c:"m = parseInt(Date.monthNumbers[results["+B+"].substring(0, 3)], 10);\n",s:"("+Date.monthNames.join("|")+")"} diff --git a/roojs-debug.js b/roojs-debug.js index b116cbfac9..279e69f782 100644 --- a/roojs-debug.js +++ b/roojs-debug.js @@ -1205,12 +1205,36 @@ document.write(dt.format(Date.patterns.ShortDate)); /** Returns the number of milliseconds between this date and date @param {Date} date (optional) Defaults to now + @param {Date} date (optional) Defaults to now + @param {String} interval A valid date interval enum value (eg. Date.DAY) @return {Number} The diff in milliseconds @member Date getElapsed */ -Date.prototype.getElapsed = function(date) { - return Math.abs((date || new Date()).getTime()-this.getTime()); +Date.prototype.getElapsed = function(date, interval) +{ + date = date || new Date(); + var ret = Math.abs(date.getTime()-this.getTime()); + switch (interval) { + + case Date.SECOND: + return Math.floor(ret / (1000)); + case Date.MINUTE: + return Math.floor(ret / (100*60)); + case Date.HOUR: + return Math.floor(ret / (100*60*60)); + case Date.DAY: + return Math.floor(ret / (100*60*60*24)); + case Date.MONTH: // this does not give exact number...?? + return ((date.format("Y") - this.format("Y")) * 12) + (date.format("m") - this.format("m")); + case Date.YEAR: // this does not give exact number...?? + return (date.format("Y") - this.format("Y")); + + case Date.MILLI: + default: + return ret; + } }; + // was in date file.. -- 2.39.2