Solved

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

Posted on 2010-09-15
2
735 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

791 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