0

Having a really weird issue I can't pinpoint. I've tried a few different variations to get the number of days between two dates in JS. For the most part (99% of the time), the below works great, but I've been getting some incorrect results **sometimes when I cross over months. For example, if I get the amount of days between July 31st, 2015 and August 1st, 2015, it correctly returns 1. However, if I try to grab the amount of days between June 30th, 2015 and July 1st, 2015 I get the result of 2 days (there are only 30 days in June, I've double checked). Is this something with my server environment or am I going crazy?

start_date = new Date(start); // assuming the format is correct
end_date = new Date(ends); // assuming the format is correct
diff_ms = end_date.getTime() - start_date.getTime(); 
var days = diff_ms / 86400000;

I've also tried the following and got the exact same results:

start_date = new Date(start); // assuming the format is correct
end_date = new Date(ends); // assuming the format is correct
var timeDiff = Math.abs(end_date.getTime() - start_date.getTime());
var days = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
GabeN
  • 11
  • 2
  • 1
    What exactly do your starting strings look like when you get strange answers? – Pointy May 02 '15 at 13:41
  • I get `1`: `var d1 = new Date(2015, 05, 30); var d2 = new Date(2015, 06, 1); (d2-d1)/86400000;`. –  May 02 '15 at 13:44
  • 1
    You could start by not assuming the format is correct. Show an example of what the actual input is. It's also a bit dangerous to assume a day has exactly 24 hours; it's not always true (think e.g. daylight savings time). – JJJ May 02 '15 at 13:49
  • Thanks for the quick answers. The dates are being pulled from an input on change. The input is in format mm-dd-yyyy and here is the first part of the script: `from = jQuery("#from").val().split("-"); start = new Date(from[2], from[0], from[1] - 1); to = jQuery("#to").val().split("-"); ends = new Date(to[2], to[0], to[1] - 1);` – GabeN May 02 '15 at 13:59
  • @GabeN the script is not as interesting as the actual date strings that are causing problems. – Pointy May 02 '15 at 13:59
  • 1
    possible duplicate of [Get difference between 2 dates in javascript?](http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – Jose Ricardo Bustos M. May 02 '15 at 14:01
  • start_date = new Date(2015, 06, 30); end_date = new Date(2015, 07, 1); I also got 2 with this one – Rodrigo López May 02 '15 at 14:01
  • @RodrigoLópez months are numbered from zero - June is 5, July is 6 – Pointy May 02 '15 at 14:03
  • Maybe he had the same confusion... – Rodrigo López May 02 '15 at 14:05

1 Answers1

0

Thanks @Pointy & @Rodrigo, you guys both led me to the problem. I was aware that months started at zero but I incorrectly wrote the initial script and subtracted a day from the **Day not from the **Month. It should have been:

from = jQuery("#from").val().split("-");
start = new Date(from[2], from[0] - 1, from[1]);
to = jQuery("#to").val().split("-");
ends = new Date(to[2], to[0] - 1, to[1]);

Thanks for the help!

GabeN
  • 11
  • 2