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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CybervanesAuthor Commented:
think that did it! thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.