Take the 2-minute tour ×

I hope someone can help me out here, I've not much clue about JS so the task at hand has grown out of proportion.

I have a html form that has 3 fields, 2 of which should be times (like 11:00, 17:00, 23:45, etc) and one is a simple number.

I have been trying to find a way to

  1. get the difference between the two times (11:00 - 13:00)
  2. convert it to decimals (so many hours like: 2, 2.5, 2.25, etc)
  3. and multiply it by the 3rd variable (17*2.5)

Any contribution, suggestion, link would be much appreciated.


share|improve this question
What have you tried ? If you try nothing, I suppose you are quite new to javascript. If this is the case, google is your friend and internet has plenty of excellent tutorial about javascript. Stack overflow is not here to copy a tutorial you can find in the first page result by typing "learn javascript" on google... –  Jerome Cance Jul 6 '12 at 11:40
Look at stackoverflow.com/questions/1787939/… –  Klas Lindbäck Jul 6 '12 at 11:46
add comment

2 Answers

To compute your time, you need to create new Date objects and then call setTime() with your subtractions.

var dt = new Date('8/24/2009 11:00:00');
var newHour = dt.getHours() + 2;
dt.setTime(newHour, 0, 0, 0);


var dt1 = new Date('8/24/2009 11:00:00');
var dt2 = new Date('8/24/2009 13:00:00');

var h1 = dt1.getHours();
var h2 = dt2.getHours();

var h = dt2.getHours() - dt1.getHours();

To convert minutes to decimal, just divide by 60.

var dt1 = new Date('8/24/2009 11:20:00');
var h = dt1.getHours();
var m = dt1.getMinutes();

h + (m/60) = 8.33

So, basically, start with real Date objects, then use the built-in methods like getHours() and getMinutes() to do your math/conversions. Should be a piece of cake, once you have the real Date objects.

share|improve this answer
Thanks Chris for your help, you're awesome!!!! –  Csongor Jul 6 '12 at 12:04
You should not expect the Date function to correctly parse a non–standard string, the fact that it works is just chance. In such cases, Date.parse is implementation dependent. There is only one string format specified in ES5 (a version of theISO8601 extended format) and none in ECMA-262 ed 3. The particular string in your answer is used by only a very small percentage of the world's population. –  RobG Jul 6 '12 at 12:08
@RobG - you're probably right, but maybe the OP is in that small percentage. Anyway, why not start off with something easy. If I was teaching someone to drive, I wouldn't start off by putting them in a race car. The point of my answer is to use methods like getHours and getMinutes to perform simple math on time, not how to parse date/time values correctly in all languages. –  Chris Gessler Jul 6 '12 at 12:18
It doesn't matter if the OP is in that percentage, it's visitors to the pages the OP develops that matter. Far better to be given accurate information from the start than have to learn it later. –  RobG Jul 6 '12 at 12:28
@RobG - And you still don't get it. Never mind... –  Chris Gessler Jul 6 '12 at 12:33
add comment

If the times are in the format specified, you can get the difference in minutes using:

function diffMins(t0, t1) {
  t0 = t0.split(':');
  t1 = t1.split(':');
  return t1[0]*60 + +t1[1] - t0[0]*60 - +t0[1]

That can be turned into decimal hours using:

function minsToHours(mins) {
  return (mins/60).toFixed(2);

alert(minsToHours(diffMins('17:00', '23:45')));  // 6.75

If you want to get the difference in decimal hours for Date objects, then:

function dateDiffInHours(d0, d1) {
  return ((d1 - d0) / 3.6e6).toFixed(2)

alert( dateDiffInHours( new Date(2012,07,02,17), new Date(2012,07,02,23,45))); // 6.75 

Please note that passing a string to the Date object is not a good idea. While most browsers will parse one or two formats, some won't. There is no standard format specified in ESCMA-262 ed 3, parsing of date strings is completely implementation dependent. ES5 specifies a modification of the ISO8601 extended format (e.g. 2012-07-02T23:45:00Z), but a good percentage of browsers in use don't support it.

So always parse the string yourself, or call the constructor using arguments per the ECMAScript specification.

share|improve this answer
Thank you ever so much, Rob! I really appreciate your help! –  Csongor Jul 6 '12 at 12:39
add comment

Your Answer


