Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a minute to hh:mm with JavaScript
    primarykey
    data
    text
    <p>I'm a PHP programmer who seldom uses Javascript, so I need help figuring out how to accomplish this with JS or JQuery (I currently have jquery-1.7.min.js). I've created three clocks showing the time in different timezones with PHP, and I'd like to use JS to increment the times every minute.</p> <p>The HTML for a clock is like this:</p> <pre><code>&lt;div class="clock"&gt; &lt;div class="pm"&gt;&lt;/div&gt; &lt;div class="time"&gt;11:59&lt;/div&gt; &lt;/div&gt; </code></pre> <p>After adding one minute it should be like this:</p> <pre><code>&lt;div class="clock"&gt; &lt;div class="am"&gt;&lt;/div&gt; &lt;div class="time"&gt;00:00&lt;/div&gt; &lt;/div&gt; </code></pre> <p>This is what I've got so far:</p> <pre><code>var myVar = setInterval(function(){setClocks()},60000); function setClocks() { $(".clock").each(function() { var ampm = $("div:first-child"); var time = $(".time"); // add one minute to time and toggle am/pm if needed time.innerHTML = newTime; } } </code></pre> <p>Any help would be greatly appreciated.</p> <p><strong>UPDATE:</strong></p> <p>I've got it, thanks to Dead133 and kei. This is my updated code:</p> <pre><code>var myVar = setInterval(function(){setClocks()}, 60000); function setClocks() { $(".clock").each(function() { var ampm = $("div:first-child", $(this)); var time = $(".time", $(this)); var parts = time.text().split(':'); var hours = parseInt(parts[0], 10); var minutes = parseInt(parts[1], 10); ++minutes; if (minutes == 60) { minutes = 0; ++hours; if (hours == 12) { ampm.toggleClass('am'); ampm.toggleClass('pm'); } } if (hours &gt; 12) hours -= 12; if (hours.toString().length &lt; 2) hours = "0" + hours; if (minutes.toString().length &lt; 2) minutes = "0" + minutes; time.text(hours + ':' + minutes); }) } </code></pre> <p>Thanks for the help, everybody! I'd done this kind of thing in PHP, so I knew the logic. It was just the JS/JQ that was hanging me up.</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. 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