html - Round up to the nearest quarter hour - JavaScript -


i'm trying create select element time shown:

            <strong>time:</strong>             <select name="hours" id="hours">                 <option value="1">1</option>                 <option value="2">2</option>                 <option value="3">3</option>                 <option value="4">4</option>                 <option value="5">5</option>                 <option value="6">6</option>                 <option value="7">7</option>                 <option value="8">8</option>                 <option value="9">9</option>                 <option value="10">10</option>                 <option value="11">11</option>                 <option value="12">12</option>                 <option value="13">13</option>                 <option value="14">14</option>                 <option value="15">15</option>                 <option value="16">16</option>                 <option value="17">17</option>                 <option value="18">18</option>                 <option value="19">19</option>                 <option value="20">20</option>                 <option value="21">21</option>                 <option value="22">22</option>                 <option value="23">23</option>                 <option value="24">24</option>             </select>             :             <select name="minutes" id="minutes">                 <option value="00">00</option>                 <option value="15">15</option>                 <option value="30">30</option>                 <option value="45">45</option>             </select> 

in javascript i'm trying show current time. current have code rounds quarter hours or down. so, 10:25 => 10:30 & 9:18 => 9:15.

i know how round instead of rounding down well.

my javascript:

var currentdate=new date();  $("#hours").val(currentdate.gethours());  $("#minutes").val((((currentdate.getminutes() + 7.5)/15 | 0) * 15) % 60); 

try code:

var minutes = (parseint((currentdate.getminutes() / 15)) + 1) * 15; minutes = minutes >= 60 ? minutes - 60 : minutes;  $("#minutes").val(minutes); 

explenation of code:

var minutes = (parseint((currentdate.getminutes() / 15)) + 1) * 15; 

this line divides current minuts 15 , parses result integer, cut off floating point, , adds +1 round up. multiplies 15 correct minutes of current quarter hour

minutes = minutes >= 60 ? minutes - 60 : minutes;  

checks if resulting minutes greater 60, if subtract 60 not "overshoot" 60 minutes

$("#minutes").val(minutes); 

sets value on given tag


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -