By Daniel · January 9, 2009
Returns a string describing the given time interval (dt) in English. For example, timediff(1000*60*60*34) will give "one day, ten hours".
/* timediff(dt)
*
* Returns a string describing the given time interval (dt) in English.
* dt should be given in milliseconds.
*
* For example, timediff(1000*60*60*34) will give "one day, ten hours".
*/
var dt, nYrs, nDays, nHrs, nMins, nSecs, nMils,
tYrs, tDays, tHrs, tMins, tSecs, tMils;
dt = abs(argument0);
nYrs = dt div 31536000000;
nDays = (dt div 86400000) mod 365;
nHrs = (dt div 3600000) mod 24;
nMins = (dt div 60000) mod 60;
nSecs = (dt div 1000) mod 60;
nMils = dt mod 1000;
tYrs = string(nYrs) + ' year' + (nYrs != 1)*'s';
tDays = string(nDays) + ' day' + (nDays != 1)*'s';
tHrs = string(nHrs) + ' hour' + (nHrs != 1)*'s';
tMins = string(nMins) + ' minute' + (nMins != 1)*'s';
tSecs = string(nSecs) + ' second' + (nSecs != 1)*'s';
tMils = string(nMils) + ' millisecond' + (nMils != 1)*'s';
if (nYrs > 0)
return (tYrs + (nDays > 0)*(', ' + tDays));
if (nDays > 0)
return (tDays + (nHrs > 0)*(', ' + tHrs));
if (nHrs > 0)
return (tHrs + (nMins > 0)*(', ' + tMins));
if (nMins > 0)
return (tMins + (nSecs > 0)*(', ' + tSecs));
if (nSecs > 0)
return (tSecs + (nMils > 0)*(', ' + tMils));
return (tMils);
Categories: Data processing, String handling
There are no comments to display.
You must be signed in to post comments.