0

actually i am making a plug-in for a website so that the support division employees don't have fill in the job sheet after every time the work on a issue and for that there are timestamps where we have to enter the hours spent on the job it kinda looks like this

start time   end time   total
---------- ---------- ----------
09 : 00 12 : 30 03 : 30
---------- ---------- ----------

and i am able to get the time for the user but not able to get the calculate the difference between the hours, here's my script for that.. kindly help.

<form id="timesheets"> 
    <div>
        <label>Item:</label>
        <select id="selectItem" name="item"><option value="123">Item 1</option></select>
    </div>
    <div>
        <!-- These textboxes should work like KReports -->
        <input id="start" class="timeInput" type='time'  value='before' />  <!-- This should default to current time minus 15 mins -->
     input id="end" class="timeInput" type='time' value='now'/> <!-- This should default to current time (to the nearest 15 mins) -->
        <input id="difference" type="time" value="Total"  />
        <script>

<script>
$(function() {
  var currentTime = new Date();
  var previousHours;
  var previousMinutes;
  var currentHours;
  var currentMinutes;
  var totalMinutes;
  var totalHours;
  var i;

  var MS_PER_MINUTE = 60000;
  var d = new Date(currentTime.valueOf() - (15 * MS_PER_MINUTE)),
    hours = d.getHours(),
    minutes = d.getMinutes();

  previousMinutes = (Math.round(minutes / 15) * 15) % 60;
  previousHours = minutes > 52 ? (hours === 23 ? 0 : ++hours) : hours;

  if (previousHours < 10) previousHours = '0' + previousHours;
  if (previousMinutes < 10) previousMinutes = '0' + previousMinutes;

  jQuery("#start").attr({
    'value': previousHours + ':' + previousMinutes
  });

  hours = currentTime.getHours();
  minutes = currentTime.getMinutes();

  currentMinutes = (Math.round(minutes / 15) * 15) % 60;
  currentHours = minutes > 52 ? (hours === 23 ? 0 : ++hours) : hours;

  if (currentHours < 10) currentHours = '0' + currentHours;
  if (currentMinutes < 10) currentMinutes = '0' + currentMinutes;

  jQuery("#end").attr({
    'value': currentHours + ':' + currentMinutes
  });

  jQuery(".timeInput").change(function() {
    //Get the date value of start
    var premin = (previousHour + (previousMinutes) / 60);
    var endValue = (premin).getMilliseconds();

    alert(endValue);
    //Get the date value of end
    var currmin = (currentHours + (currentMinutes) / 60);
    var startValue = (currmin).getMilliseconds();
    //Convert both to milliseconds
    //Calculate milliseconds difference
    //Convert milliseconds to hours and minutes
  });



  $('input[type="time"][value="Total"]').each(function() {

    totalHours = currentHours - previousHours;
    if (currentHours = previousHours + 1) {
      totalHours = '00';
    }
    totalMinutes = currentMinutes - previousMinutes;
    if (totalMinutes < 0) {
      totalMinutes = 60 - (Math.abs(totalMinutes));
    }

    $(this).attr({
      'value': totalHours + ':' + totalMinutes
    });
  });
});
</script>

0 Answers0