foxwhisper
26 Jan 2009, 10:58 AM
So like.. I spent ages searching around on the intarwebz for a piece of code which would convert milliseconds to a human readable string (0 days, 0 hrs, 0 mins, 0 secs) etc, but could I find one? No lol. So I wrote one instead :D
To use the below, use it exactly as you would with getElapsed. The only difference being, it will return a human readable string, rather than the number of milliseconds.
Here is an example of using it with MySQL Dates
Date1='2009-01-01 01:01:01';
Date2='2009-01-02 01:01:02';
return new Date.parseDate(Date1, "Y-m-d H:i:s").getElapsedHuman(new Date.parseDate(Date2,"Y-m-d H:i:s"));
Maybe it should be extended to say 'minus' days etc, if the second date is higher than the first date?? I dunno.. For now, here it is :D Enjoy!
Ext.override(Date, {
getElapsedHuman : function(otherDate) {
try {
ms = this.getElapsed(otherDate)/1000;
} catch (Exception) {
throw "Invalid Comparison Date: "+Exception
}
// Instantiate counters
days=0;
hrs=0;
mins=0;
secs=0;
out=[];
// Check how many days
while (ms>=86400) {
days++;
ms-=86400;
}
// Check how many hours
while (ms>=3600) {
hrs++;
ms-=3600;
}
// Check how many minutes
while (ms>=60) {
mins++;
ms-=60;
}
// Check how many seconds
while (ms>0) {
secs++;
ms-=1;
}
// Append each one into the out buffer
if (days) { out.push(days+" days") }
if (hrs) { out.push(hrs+" hrs") }
if (mins) { out.push(mins+" mins") }
if (secs) { out.push(secs+" secs") }
// Then join each one together, and boom! :)
return out.join(", ");
}
});
To use the below, use it exactly as you would with getElapsed. The only difference being, it will return a human readable string, rather than the number of milliseconds.
Here is an example of using it with MySQL Dates
Date1='2009-01-01 01:01:01';
Date2='2009-01-02 01:01:02';
return new Date.parseDate(Date1, "Y-m-d H:i:s").getElapsedHuman(new Date.parseDate(Date2,"Y-m-d H:i:s"));
Maybe it should be extended to say 'minus' days etc, if the second date is higher than the first date?? I dunno.. For now, here it is :D Enjoy!
Ext.override(Date, {
getElapsedHuman : function(otherDate) {
try {
ms = this.getElapsed(otherDate)/1000;
} catch (Exception) {
throw "Invalid Comparison Date: "+Exception
}
// Instantiate counters
days=0;
hrs=0;
mins=0;
secs=0;
out=[];
// Check how many days
while (ms>=86400) {
days++;
ms-=86400;
}
// Check how many hours
while (ms>=3600) {
hrs++;
ms-=3600;
}
// Check how many minutes
while (ms>=60) {
mins++;
ms-=60;
}
// Check how many seconds
while (ms>0) {
secs++;
ms-=1;
}
// Append each one into the out buffer
if (days) { out.push(days+" days") }
if (hrs) { out.push(hrs+" hrs") }
if (mins) { out.push(mins+" mins") }
if (secs) { out.push(secs+" secs") }
// Then join each one together, and boom! :)
return out.join(", ");
}
});