Finding The Difference Between To Times | JS / jQuery

I'm trying to develop a function to calculate the different between to times in hours. these times will never be more than 24 hours apart.

e.g. 10:45 - 21:30 = 11.45hrs

I know I'm getting close but i just cant seem to nail it...

<script>
function calcHours(ID){
	et = $('#End-Time', '#'+ID).val();
	substr = et.split(':');
	eth = parseFloat(substr[0]);
	etm = parseFloat(substr[1]);
	st = $('#Start-Time', '#'+ID).val();
	substr = st.split(':');
    sth = parseFloat(substr[0]);
	stm = parseFloat(substr[1]);
	if(etm==0){ etm=60; }
	m = ((stm-60)+etm)/60;
	h = eth-sth;
	time = h+m;
	$('#Hours' , '#'+ID).val(time);
	//alert('sth='+sth+' stm'+stm+' eth'+eth+' etm'+etm+' h'+h+' m'+m+' time'+time);
}
</script>

Open in new window


anybody have any ideas?

thanks in advance!
LVL 2
CybervanesAsked:
Who is Participating?
 
StingRaYCommented:
Ah! I've tried that in the internal browser in my editor. I tested it in FF and safari, both return 0 instead of 8!! Weird!!

Try this, then:

function calcHours(ID){
        et = $('#End-Time', '#'+ID).val();
        substr = et.split(':');
        eth = parseFloat(substr[0]);
        etm = parseFloat(substr[1]);
        st = $('#Start-Time', '#'+ID).val();
        substr = st.split(':');
                sth = parseFloat(substr[0]);
        stm = parseFloat(substr[1]);

                hdiff = eth - sth;
                mdiff = etm - stm;
                if (mdiff < 0) {
                        hdiff--;
                        mdiff+=60;
                }
       //alert('sth='+sth+' stm'+stm+' eth'+eth+' etm'+etm+' h'+h+' m'+m+' time'+time);
           alert(hdiff+'hr. '+mdiff+'min');
}

Open in new window


I change parseInt to parseFloat.
0
 
StingRaYCommented:
function calcHours(ID){
        et = $('#End-Time', '#'+ID).val();
        substr = et.split(':');
        eth = parseInt(substr[0]);
        etm = parseInt(substr[1]);
        st = $('#Start-Time', '#'+ID).val();
        substr = st.split(':');
		sth = parseInt(substr[0]);
        stm = parseInt(substr[1]);

		hdiff = eth - sth;
		mdiff = etm - stm;
		if (mdiff < 0) {
			hdiff--;
			mdiff+=60;
		}
       //alert('sth='+sth+' stm'+stm+' eth'+eth+' etm'+etm+' h'+h+' m'+m+' time'+time);
	   alert(hdiff+'hr. '+mdiff+'min');
}

Open in new window

0
 
CybervanesAuthor Commented:
nope that doesn't work either.... e.g. st 8:30 et 12:45 returns `12 hr. 15 min`.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
StingRaYCommented:
Mine returns 4hr. 15min

function calcHours(ID){
		et = "12:45"; // 
		st = "8:30";  //

       // et = $('#End-Time', '#'+ID).val();
        substr = et.split(':');
        eth = parseInt(substr[0]);
        etm = parseInt(substr[1]);
        //st = $('#Start-Time', '#'+ID).val();
        substr = st.split(':');
		sth = parseInt(substr[0]);
        stm = parseInt(substr[1]);

		hdiff = eth - sth;
		mdiff = etm - stm;
		if (mdiff < 0) {
			hdiff--;
			mdiff+=60;
		}
       //alert('sth='+sth+' stm'+stm+' eth'+eth+' etm'+etm+' h'+h+' m'+m+' time'+time);
	   alert(hdiff+'hr. '+mdiff+'min');
}

Open in new window

0
 
CybervanesAuthor Commented:
its the leading zeros in my test... et = "12:45";  st = "08:30";

why would that be screwing it up?
0
 
StingRaYCommented:
whether there is the leading zero or not, it returns 4hr. 15min
0
 
StingRaYCommented:
Can you print out eth,etm,sth,stm,hdiff,mdiff?
0
 
CybervanesAuthor Commented:
are you sure? my test returns sth as 0 not 08

	et = "12:45"; // 
		st = "08:30";  //

        //et = $('#End-Time', '#'+ID).val();
        substr = et.split(':');
        eth = parseInt(substr[0]);
        etm = parseInt(substr[1]);
        //st = $('#Start-Time', '#'+ID).val();
        substr = st.split(':');
		sth = parseInt(substr[0]);
        stm = parseInt(substr[1]);

		hdiff = eth - sth;
		mdiff = etm - stm;
		if (mdiff < 0) {
			hdiff--;
			mdiff+=60;
		}
       //alert('sth='+sth+' stm'+stm+' eth'+eth+' etm'+etm+' h'+h+' m'+m+' time'+time);
	   alert('st='+st+' sth='+sth+' et='+et+' dif='+hdiff+'hr. '+mdiff+'min');

Open in new window

0
 
CybervanesAuthor Commented:
st=08:30 sth=0 et=12:45 dif=12hr. 15min
Can you print out eth,etm,sth,stm,hdiff,mdiff?
0
 
StingRaYCommented:
I use the code in your last reply, it says:

st=08:30 sth=8 et=12:45 dif=4hr. 15min
0
 
CybervanesAuthor Commented:
weird... maybe is a browser thing. im using FF for MAC
0
 
CybervanesAuthor Commented:
nope same result in safari
0
 
CybervanesAuthor Commented:
think that did it! thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.