Nate
Nate

Reputation: 11

JavaScript date difference correction

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.

Upvotes: 0

Views: 150

Answers (3)

Hardy
Hardy

Reputation: 5631

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];
}

Upvotes: 0

Yogesh
Yogesh

Reputation: 2228

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.

Upvotes: 0

Peter Olson
Peter Olson

Reputation: 142911

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)

Upvotes: 1

Related Questions