js count 15 min interval (double)

167 views Asked by At

In JS i've a var {double} hour, where i want to know how many 15 minutes are in there.

// Example
var hour = 2.30
// Example calculation
// 1 hour = 60*60 = 3600
// 2 hour = 60*120 = 7200
// 15 min = 60 * 15 = 900
// 30 min = 60 * 30 = 1800
// count 15min within hour
var result = 7200 + 1800 = 9000 / 900 = 10

I can count the hours like this

var hour = 2.30
var tmp_hour = parseInt((hour.toString().split('.')[0]) * 60 * 60);
// tmp_hour = 7200

But i can't find a way to calculate the fractioanal part to minutes, see code

// this only working with::
// hour = 2.15, 2.45

// hour 2.15 => result => 900 (correct)
// hour 2.45 => result => 2700 (correct)
// hour 2.30 => result => 180 (not correct)
var tmp_min  = parseInt(hour.toString().split('.')[1] / 15 * 900);

Does someone nows a better way for calculating this.

And i've another question whats could be offtopic.

I have a table with a couple of cells. When i click on a cell its get divided into four pieces (time interval, where every piece stands for 15 min).

// example
<table>
   <tr>
      <td></td>
      <td></td>
      <td>
         <div class="divider">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
         </div>
      </td>
      <td></td>
   </tr>
</table>

This is working fine, but i want to calculate the width for every label (4 pieces).

For instance if the clicked cell has a width of 17 px. I want to following width

// span 1: 4px    
// span 2: 5px    
// span 3: 4px    
// span 4: 4px 
// total: 17px       

I've tried several calculation with mod and dividing but could not get the disired result.

0

There are 0 answers