Note that there are some explanatory texts on larger screens.

plurals
  1. POProcessing formatted time with Javascript
    primarykey
    data
    text
    <p>Lets say I have start of and duration of a TV program with this format: <code>10:05</code>.<br /> What I want to do is when someone is entering programs data and he enters start time and duration of a program, my code will add them together and fill the next program's start time automatically.</p> <p>I was trying to convert these times to seconds and then after manipulation convert them to formatted time again, but I don't know how can I do that.</p> <pre><code>function processTime(time) { if (typeof time !== 'undefined') { var times = time.split(":"); var minutes = times[0]; var seconds = times[1]; seconds = parseInt(seconds, 10) + (parseInt(minutes, 10) * 60); } else { var seconds = null; } return seconds; } function createTime(timestamp) { var output; if (typeof timestamp !== 'undefined') { // Problem is here, my timestamp is not standard unix timestamp var time = new Date(timestamp * 1000); var minutes = time.getMinutes(); var seconds = time.getSeconds(); output = minutes + ":" + seconds; } return output; } $progForm.delegate(".program-duration", 'keyup', function() { $(this).on('focusout', function() { var duration = processTime($(this).val()); var startTime = processTime($(this).parent().prev().find(".program-time").val()); if (duration &amp;&amp; typeof duration !== 'undefined' &amp;&amp; startTime &amp;&amp; typeof startTime !== 'undefined') { var nextStart = duration + startTime; var $nextProgramTime = $(this).parent().parent().next().find(".program-time"); if (parseInt($nextProgramTime.val()) &lt; 1) { $nextProgramTime.val(createTime(nextStart)); } } }); }); </code></pre> <p><br /></p> <pre><code>&lt;form&gt; &lt;table&gt; &lt;tbody class="programs-inputs"&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="program-time timepicker input-mini" name="programs[time][]" placeholder="00:00" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="program-duration timepicker input-mini" name="programs[duration][]" placeholder="00:00" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="program-time timepicker input-mini" name="programs[time][]" placeholder="00:00" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="program-duration timepicker input-mini" name="programs[duration][]" placeholder="00:00" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/form&gt; </code></pre>
    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.
 

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