Solved

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

Posted on 2010-09-15
2
739 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

688 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