Difference between revisions of "Convert Seconds to Human Readable Duration"
From Hawk Wiki
(Created page with "<pre class="brush:javascript"> /** * Convert to human readable time duration * @param seconds * @param desiredUnit {string} The desired unit. can be "W", "D", "H", "M"...") |
|||
Line 1: | Line 1: | ||
<pre class="brush:javascript"> | <pre class="brush:javascript"> | ||
/** | /** | ||
− | + | * Convert to human readable time duration | |
− | + | * @param seconds | |
− | + | * @param desiredUnit {string} The desired unit. can be "W", "D", "H", "M" | |
− | + | * unitCount {number} How many units to show | |
− | + | * @returns { | |
− | + | * value: {number} seconds input, | |
− | + | * text: {string} "Time remaining text", | |
− | + | * secondsLeft: {number} seconds left not showing in the text | |
− | + | * } | |
− | + | * (ignore the unit smaller than the 2nd unit. if desiredUnit = "D", unitCount = 2, 1d1m show "1d") | |
− | + | */ | |
− | + | var toHumanReadableDuration = function (seconds, desiredUnit, unitCount) { | |
var that = this; | var that = this; | ||
/** | /** | ||
Line 22: | Line 22: | ||
var numberWithUnit = function (number, unitName) { | var numberWithUnit = function (number, unitName) { | ||
return number + unitName; | return number + unitName; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}; | }; | ||
// General function to return data based on unit and secondsInUnit | // General function to return data based on unit and secondsInUnit | ||
var getTimeByUnit = function (secondsTmp, unit, secondsInUnit) { | var getTimeByUnit = function (secondsTmp, unit, secondsInUnit) { | ||
− | var ret = | + | var ret = Math.floor(secondsTmp / secondsInUnit); |
return { | return { | ||
value: ret, | value: ret, | ||
text: ret > 0 ? numberWithUnit(ret, unit) : "", | text: ret > 0 ? numberWithUnit(ret, unit) : "", | ||
// How many seconds left after Math.floor. Means how many second is omitted | // How many seconds left after Math.floor. Means how many second is omitted | ||
− | secondsLeft: | + | secondsLeft: secondsTmp - ret * secondsInUnit |
}; | }; | ||
}; | }; | ||
− | var | + | var ret = { |
− | + | value: seconds, | |
− | + | text: "", | |
− | + | secondsLeft: 0 | |
− | + | ||
− | + | ||
− | + | ||
}; | }; | ||
+ | //If the duration is <=0, just return | ||
+ | if (seconds <= 0) { | ||
+ | return ret; | ||
+ | } | ||
+ | // Call corresponding function based on the unit param | ||
+ | var unitFuncMappings = [{ | ||
+ | "unit": "W", | ||
+ | "secondsInUnit": 604800 //seconds in a week | ||
+ | }, { | ||
+ | "unit": "D", | ||
+ | "secondsInUnit": 86400 | ||
+ | }, { | ||
+ | "unit": "H", | ||
+ | "secondsInUnit": 3600 | ||
+ | }, { | ||
+ | "unit": "M", | ||
+ | "secondsInUnit": 60 | ||
+ | }, { | ||
+ | "unit": "S", | ||
+ | "secondsInUnit": 1 | ||
+ | }]; | ||
− | + | // Find the index of desired unit | |
− | + | var unitIndex = 0, | |
− | + | i; | |
− | + | for (i = 0; i < unitFuncMappings.length; i++) { | |
− | + | if (unitFuncMappings[i].unit == unit) { | |
− | + | unitIndex = i; | |
− | + | break; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
+ | } | ||
− | + | // Get the time format for desired unitCount | |
− | + | for (i = unitIndex; i < unitFuncMappings.length; i++) { | |
− | + | if (getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit).value > 0) { | |
− | + | for (var j = 0; j < unitCount; j++) { | |
− | + | if (unitFuncMappings[i + j]) { | |
− | + | var retTmp = getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit); | |
− | + | if (j > 0) { | |
− | + | ret.text += " "; // add a space between time units | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
+ | ret.text += retTmp.text; | ||
+ | ret.secondsLeft = retTmp.secondsLeft; | ||
+ | seconds = retTmp.secondsLeft; //refresh seconds for next calculation | ||
} | } | ||
− | |||
} | } | ||
+ | break; | ||
} | } | ||
− | + | } | |
− | + | return ret; | |
+ | } | ||
</pre> | </pre> |
Latest revision as of 01:31, 28 April 2015
/** * Convert to human readable time duration * @param seconds * @param desiredUnit {string} The desired unit. can be "W", "D", "H", "M" * unitCount {number} How many units to show * @returns { * value: {number} seconds input, * text: {string} "Time remaining text", * secondsLeft: {number} seconds left not showing in the text * } * (ignore the unit smaller than the 2nd unit. if desiredUnit = "D", unitCount = 2, 1d1m show "1d") */ var toHumanReadableDuration = function (seconds, desiredUnit, unitCount) { var that = this; /** * Return a text with number and unit * @param number the number * @param unitName the unit text * @returns {*} */ var numberWithUnit = function (number, unitName) { return number + unitName; }; // General function to return data based on unit and secondsInUnit var getTimeByUnit = function (secondsTmp, unit, secondsInUnit) { var ret = Math.floor(secondsTmp / secondsInUnit); return { value: ret, text: ret > 0 ? numberWithUnit(ret, unit) : "", // How many seconds left after Math.floor. Means how many second is omitted secondsLeft: secondsTmp - ret * secondsInUnit }; }; var ret = { value: seconds, text: "", secondsLeft: 0 }; //If the duration is <=0, just return if (seconds <= 0) { return ret; } // Call corresponding function based on the unit param var unitFuncMappings = [{ "unit": "W", "secondsInUnit": 604800 //seconds in a week }, { "unit": "D", "secondsInUnit": 86400 }, { "unit": "H", "secondsInUnit": 3600 }, { "unit": "M", "secondsInUnit": 60 }, { "unit": "S", "secondsInUnit": 1 }]; // Find the index of desired unit var unitIndex = 0, i; for (i = 0; i < unitFuncMappings.length; i++) { if (unitFuncMappings[i].unit == unit) { unitIndex = i; break; } } // Get the time format for desired unitCount for (i = unitIndex; i < unitFuncMappings.length; i++) { if (getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit).value > 0) { for (var j = 0; j < unitCount; j++) { if (unitFuncMappings[i + j]) { var retTmp = getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit); if (j > 0) { ret.text += " "; // add a space between time units } ret.text += retTmp.text; ret.secondsLeft = retTmp.secondsLeft; seconds = retTmp.secondsLeft; //refresh seconds for next calculation } } break; } } return ret; }