0

I have a function that will calculate time between two date / time but I am having a small issue with the return.

Here is the way I collect the information.

Start Date Start Time Ending Date Ending Time Hours

And here is the function that calculates the dates and times:

function calculate (form) {
    var d1 = document.getElementById("date1").value;
    var d2 = document.getElementById("date2").value;
    var t1 = document.getElementById("time1").value;
    var t2 = document.getElementById("time2").value;
    var dd1 = d1 + " " + t1;
    var dd2 = d2 + " " + t2;
    var date1 = new Date(dd1);
    var date2 = new Date(dd2);
    var sec = date2.getTime() - date1.getTime();
    if (isNaN(sec)) {
        alert("Input data is incorrect!");
        return;
    }
    if (sec < 0) {
        alert("The second date ocurred earlier than the first one!");
        return;
    }
    var second = 1000,
        minute = 60 * second,
        hour = 60 * minute,
        day = 24 * hour;
    var hours = Math.floor(sec / hour);
    sec -= hours * hour;
    var minutes = Math.floor(sec / minute);
    sec -= minutes * minute;
    var seconds = Math.floor(sec / second);
    var min = Math.floor((minutes * 100) / 60);
    document.getElementById("result").value = hours + '.' + min;
}


If I put in todays date for both date fields and then 14:30 in the first time field and 15:35 in the second time field the result is shown as 1.8 and it should be 1.08

I didn't write this function but I am wondering if someone could tell me how to make that change?

Thank you.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Nate
  • 11
  • 1
  • 5
  • asked before.. many times: http://stackoverflow.com/questions/10696654/get-total-time-difference-between-two-dates-using-php – Hardy Dec 10 '13 at 01:29
  • 1
    @Hardy true, it has, but that particular question is a bad example. It's in PHP. – markasoftware Dec 10 '13 at 01:37
  • yea, divide it by one :) – Hardy Dec 10 '13 at 01:39
  • 1
    Try this: http://stackoverflow.com/questions/13413568/finding-difference-between-two-dates-times-in-javascript/13413718#13413718 – Hardy Dec 10 '13 at 01:43
  • that was just joke.. for @Doorhandle comment.."divide it by 1" what happens when you divide number by 1 = nothing :) – Hardy Dec 10 '13 at 01:45
  • @Hardy Typo :P You could put **document.getElementById("result").value = hours + '.' + min / 10** which would turn .8 to .08 – Cilan Dec 10 '13 at 01:48
  • @Hardy Yea :P I'm just going to say it was a typo, I was thinking 10 but I put 1 – Cilan Dec 10 '13 at 02:17

3 Answers3

1

If I understand correctly, the only issue you are having is that the minutes are not padded by zeroes. If this is the case, you can pad the value of min with zeroes using this little trick:

("00" + min).slice(-2)
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
0

Verify if number of minutes is less than 10 and if it is then append an additional zero in front. Follow similar approach for seconds.

Yogesh
  • 2,198
  • 1
  • 19
  • 28
0

I can't see why 15:35 - 14:30 = 1.08 is useful?

Try this instead:

function timediff( date1, date2 ) {
      //Get 1 day in milliseconds
      var one_day=1000*60*60*24;

      // Convert both dates to milliseconds
      var date1_ms = date1.getTime();
      var date2_ms = date2.getTime();

      // Calculate the difference in milliseconds
      var difference_ms = date2_ms - date1_ms;
      //take out milliseconds
      difference_ms = difference_ms/1000;
      var seconds = Math.floor(difference_ms % 60);
      difference_ms = difference_ms/60; 
      var minutes = Math.floor(difference_ms % 60);
      difference_ms = difference_ms/60; 
      var hours = Math.floor(difference_ms % 24);  
      var days = Math.floor(difference_ms/24);

      return [days,hours,minutes,seconds];
    }

function calculate (form) {
    var d1 = document.getElementById("date1").value;
    var d2 = document.getElementById("date2").value;
    var t1 = document.getElementById("time1").value;
    var t2 = document.getElementById("time2").value;
    var dd1 = d1 + " " + t1;
    var dd2 = d2 + " " + t2;
    var date1 = new Date(dd1);
    var date2 = new Date(dd2);

    var diff = timediff(date1, date2);

    document.getElementById("result").value = diff[1] + ':' + diff[2];
}
Hardy
  • 5,590
  • 2
  • 18
  • 27