Improve company productivity with a Business Account.Sign Up

x
?
Solved

How to calculate the difference between two times for the same day in hours

Posted on 2010-09-15
2
Medium Priority
?
759 Views
Last Modified: 2013-11-11
I've been searching all over the web and can't quite find the solution to what I need. I would like to calculate the difference between two times formatted in military time in hours (with two decimal places). The start and end times will be entered as text into a form and the difference (or total time below) is needed as a value for import into our work ticket system. It will be assumed that the times entered will always be on the same date, so it may be useful to return an error if the end time is prior to the start time.

For example:

Start time: 10:00 (manually entered)
End time: 13:30 (manually entered)
Total time: 1.50 hours (calculated upon form submit)

Any help would be greatly appreciated. Thanks in advance!
0
Comment
Question by:pwcit
2 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 33689148
you can set the time into the date object using the following method

function parseIntoTime( inputStrTime )
{
   var times = inputStrTime.split(":");
   var date = new Date();
   date.setHours( parseInt(times[0]) );
   date.setMinutes( parseInt(times[1]) );
  return date;
}

function timeDifference()
{
   var inputTime1 = document.getElementById("textTime1").value;
   var inputTime2 = document.getElementById("textTime2").value;
   var noOfMS = parseIntoTime( inputTime2 ).getTime() - parseIntoTime( inputTime1 ).getTime();
   alert( "number of hours are: " + (noOfMS/(1000*60*60)) );
}
0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33689155
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question