Member Avatar for david.roun.7_1

Hi all. I went through 10 pages and couldn't find anything like this, although I would have thought I would. I'm simply trying to get the difference between two times. Here's my code, any thoughts on how I can fix this would be appreciated.

<html>
<title>timesheet</title>
<head>
<script>
function addhours()
{


var shours1=document.getElementById('start1').value;
var ehours1=document.getElementById('end1').value;
var time1=new Date();
var time1a=time1.getTime(shours1);
var time1b=time1.getHours(time1a);
var time2=new Date();
var time2a=time2.getTime(ehours1);
var time2b=time1.getHours(time2a);

var diff= time2a-time1a;
document.text.hours.value=diff;
}

</script>
</head>

<body>

<form name="text">
<table>
<tr><td><br><br></td></tr>
<tr><td>Date:<input type="date" step=".5" name=""></td></tr>
<tr><td>Start time:<input type="text" id="start1" onChange="addhours()"></td><td>End time:<input type="text" id="end1" onChange="addhours()"></td><td></tr>
<tr><td>Difference:<input type="text" name="hours"></td></tr>
</body>
</html>

Hi,
Try to change your script with this:

function addhours()
{
    var diff_days  = 0;   
    var diff_hours = 0;    
    var shours1=document.getElementById('start1').value;
    var ehours1=document.getElementById('end1').value;
    //start time
    var time1=new Date(shours1);
    var time1a=time1.getTime();
    //end time
    var time2=new Date(ehours1);
    var time2a=time2.getTime();

    //Difference in hours
    var diff_x = Math.floor((time2a - time1a)/3600000);
    alert(diff_x);
    //but javascript return a number great that 744 if 
    // the difference is over 24 hours
     if (diff_x >= 744)
     {
         //must make a little calculus
         diff_days = Math.floor(diff_x/744)*24;              
         diff_hours = diff_x -  Math.floor(diff_x/744)*744;
         diff = diff_days + diff_hours;          
     }
     else
     {
        diff = diff_x;
     }
    document.text.hours.value = diff;
}
Member Avatar for david.roun.7_1

I appreciate your help. When I put in the additions (I don't need to worry about going over 24 hours so I didn't use that part), the page returns NaN. Any ideas?

Ok, change your script with that:

<script>
    function addhours()
    {
        var diff_days  = 0;
        var diff_hours = 0;
        var mydate = document.getElementById('mydate').value;
        var shours1=document.getElementById('start1').value;
        var ehours1=document.getElementById('end1').value;
        //start time
        var time1=new Date(mydate+" "+shours1);
        alert(time1);
        var time1a=time1.getTime();
        //end time
        var time2=new Date(mydate+" "+ehours1);
        alert(time2);
        var time2a=time2.getTime();
        var diff_x = Math.floor((time2a - time1a)/3600000);

        document.text.hours.value = diff;
    }
    </script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.