Solved

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

Posted on 2010-09-15
2
727 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
Comment Utility
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:ddsh79
Comment Utility
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now