Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Both solutions below use <strong>javascript</strong> (I had no idea the solution was language agnostic!). Both solutions will need to be extended if capturing durations <code>&gt; 1 month</code>.</p> <h3>Solution 1: Use the Date object</h3> <pre class="lang-js prettyprint-override"><code>var date = new Date(536643021); var str = ''; str += date.getUTCDate()-1 + " days, "; str += date.getUTCHours() + " hours, "; str += date.getUTCMinutes() + " minutes, "; str += date.getUTCSeconds() + " seconds, "; str += date.getUTCMilliseconds() + " millis"; console.log(str); </code></pre> <p>Gives:</p> <pre class="lang-js prettyprint-override"><code>"6 days, 5 hours, 4 minutes, 3 seconds, 21 millis" </code></pre> <p>Libraries are helpful, but why use a library when you can re-invent the wheel! :)</p> <h3>Solution 2: Write your own parser</h3> <pre class="lang-js prettyprint-override"><code>var getDuration = function(millis){ var dur = {}; var units = [ {label:"millis", mod:1000}, {label:"seconds", mod:60}, {label:"minutes", mod:60}, {label:"hours", mod:24}, {label:"days", mod:31} ]; // calculate the individual unit values... units.forEach(function(u){ millis = (millis - (dur[u.label] = (millis % u.mod))) / u.mod; }); // convert object to a string representation... var nonZero = function(u){ return dur[u.label]; }; dur.toString = function(){ return units .reverse() .filter(nonZero) .map(function(u){ return dur[u.label] + " " + (dur[u.label]==1?u.label.slice(0,-1):u.label); }) .join(', '); }; return dur; }; </code></pre> <p>Creates a "duration" object, with whatever fields you require. Formatting a timestamp then becomes simple...</p> <pre class="lang-js prettyprint-override"><code>console.log(getDuration(536643021).toString()); </code></pre> <p>Gives:</p> <pre class="lang-js prettyprint-override"><code>"6 days, 5 hours, 4 minutes, 3 seconds, 21 millis" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload