Solved

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

Posted on 2010-09-15
2
733 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:
gurvinder372 earned 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

810 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