Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By using the <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp" rel="noreferrer">Date</a> object and its milliseconds value, differences can be calculated:</p> <pre class="lang-javascript prettyprint-override"><code>var a = new Date(); // Current date now. var b = new Date(2010, 0, 1, 0, 0, 0, 0); // Start of 2010. var d = (b-a); // Difference in milliseconds. </code></pre> <p>You can get the number of seconds (as a integer/whole number) by dividing the milliseconds by 1000 to convert it to seconds then converting the result to an integer (this removes the fractional part representing the milliseconds):</p> <pre class="lang-javascript prettyprint-override"><code>var seconds = parseInt((b-a)/1000); </code></pre> <p>You could then get whole <code>minutes</code> by dividing <code>seconds</code> by 60 and converting it to an integer, then <code>hours</code> by dividing <code>minutes</code> by 60 and converting it to an integer, then longer time units in the same way. From this, a function to get the maximum whole amount of a time unit in the value of a lower unit and the remainder lower unit can be created:</p> <pre class="lang-javascript prettyprint-override"><code>function get_whole_values(base_value, time_fractions) { time_data = [base_value]; for (i = 0; i &lt; time_fractions.length; i++) { time_data.push(parseInt(time_data[i]/time_fractions[i])); time_data[i] = time_data[i] % time_fractions[i]; }; return time_data; }; // Input parameters below: base value of 72000 milliseconds, time fractions are // 1000 (amount of milliseconds in a second) and 60 (amount of seconds in a minute). console.log(get_whole_values(72000, [1000, 60])); // -&gt; [0,12,1] # 0 whole milliseconds, 12 whole seconds, 1 whole minute. </code></pre> <p>If you're wondering what the input parameters provided above for the second <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp" rel="noreferrer">Date object</a> are, see their names below:</p> <pre class="lang-javascript prettyprint-override"><code>new Date(&lt;year&gt;, &lt;month&gt;, &lt;day&gt;, &lt;hours&gt;, &lt;minutes&gt;, &lt;seconds&gt;, &lt;milliseconds&gt;); </code></pre> <p>As noted in the comments of this solution, you don't necessarily need to provide all these values unless they're necessary for the date you wish to represent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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