0

I've got a bit of code here that calculates the hours difference between two datetimes. Kind of at a loss here. Code is hit or miss and I'm not sure why.

var date = tr.find('td:eq(10) input').val();
var time = tr.find('td:eq(10) option:selected').val();
var d1 = parseDate(date,time);
date = tr.find('td:eq(11) input').val();
time = tr.find('td:eq(11) option:selected').val();
var d2 = parseDate(date,time);
var diff = d2.getTime() - d1.getTime();
var hoursTd = tr.find('td:eq(12)');
hoursTd.html((diff/3600000).toFixed(2));

date and time are populated correctly, pulling from a jQuery UI datepicker and a simple dropdown with values 2:30 pm, 2:45 pm, 3:00 pm, etc. It works fine when the date is the same, but sometimes adds a magnitude of ten when the date is not the same. For example:

01/11/2011 10:30 am - 01/11/2011 11:00 am = 0.50 (Correct)

01/10/2011 10:30 am - 01/11/2011 11:00 am = 24.50 (Correct)

01/09/2011 10:30 am - 01/11/2011 11:00 am = 264.50 (Incorrect)

01/08/2011 10:30 am - 01/11/2011 11:00 am = 264.50 (Incorrect)

01/07/2011 10:30 am - 01/11/2011 11:00 am = 96.50 (Correct)

Edit, Oh my, I'm sorry. Didn't include the parseDate function.

// Date m/d/Y Time h:m a
function parseDate(date,time) {

date = date.split("/");
time = time.split(" ");
hm = time[0].split(':');
if (parseInt(hm[0]) == 12) {
    hm[0] = 0;
}
if (time[1] == 'pm') {
    hm[0] = parseInt(hm[0]) + 12;
} else {
    hm[0] = parseInt(hm[0]);
}
return new Date(
    parseInt(date[2]), parseInt(date[0])-1, parseInt(date[1]),
    hm[0], parseInt(hm[1])
);
}

Edit, ok, so parseInt is the culprit. parseInt("09") returns 0. So does "08". Strangely enough, parseInt("07") returns 7. And 1-6 return correctly as well. Someone tell Javascript.

savinger
  • 6,544
  • 9
  • 40
  • 57
  • Mention the full code containing all function definitions... – AbhiRoczz... Jan 06 '12 at 17:43
  • Do some more debugging: show/alert/log the values for d1 and d2 so you're sure that your input settings are being read correctly. Also, you may want to ensure that diff is a positive number. – Larry K Jan 06 '12 at 17:44
  • May be this can help you - http://stackoverflow.com/questions/1576753/parse-date-string-in-javascript – AbhiRoczz... Jan 06 '12 at 17:45
  • Take a look at the date.js library. Handy! – Diodeus - James MacFarlane Jan 06 '12 at 19:09
  • It might be best to post an example page exhibiting the behavior. I would do console outputs to ensure your parseDate method is getting a correct value for d1/d2. – Tracker1 Jan 06 '12 at 19:10
  • I noticed in your code that the example 01/09/2011 10:30 am - 01/11/2011 11:00 am = 264.50 gives the wrong answer, but it seems that 01/9/2011 10:30 am - 01/11/2011 11:00 am = 48.50 gives the correct one. I haven't checked the code yet but have you looked into the leading zeros? – j08691 Jan 06 '12 at 19:15

2 Answers2

1

The problem is that your parseInt() on date[1] is using the leading zero.

return new Date(
    parseInt(date[2]), parseInt(date[0])-1, parseInt(date[1]),
    hm[0], parseInt(hm[1])
);

When you pass a date like 01/09/2011, the 09 is being parsed as 0, not 9.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • I think you may be right. But how would you explain this? 06/09/2010 11:30 am -- Mon May 31 2010 11:30:00 GMT-0400 (EDT), 06/07/2010 11:30 am -- Mon Jun 07 2010 11:30:00 GMT-0400 (EDT) Can I not format comments? Anyway, it parses the 09 incorrectly but the 07 correctly? – savinger Jan 06 '12 at 19:23
  • Wow that's interesting. parseInt("09") returns 0 but parseInt("07") returns 7. – j08691 Jan 06 '12 at 19:28
  • 1
    Change your parseInts to specify the radix. E.g. parseInt(date[1],10) See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt for more. – j08691 Jan 06 '12 at 19:36
0

date.js solves your problem:

var first = Date.parse(01/09/2011 10:30 am);
var second = Date.parse(01/11/2011 11:00 am);
var diffMs = Math.abs(first - second) // difference in milliseconds
matsev
  • 32,104
  • 16
  • 121
  • 156