2

So I'm playing around JavaScript's Date object, and I ran into something I think is a little strange.
I'm trying to figure out how many days there is between 2 given dates, and for that I use the formula below:

var oneDay = 24*60*60*1000;
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));

If you take 2017-05-28 & 2017-05-30 it returns 2 days - as it should

var oneDay = 24*60*60*1000;
var firstDate = new Date(2017, 05, 28);
var secondDate = new Date(2017, 05, 30);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));

If you take 2017-05-30 & 2017-06-01 it returns 1 days - supposed to be 2 days

var oneDay = 24*60*60*1000;
var firstDate = new Date(2017, 05, 28);
var secondDate = new Date(2017, 05, 30);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));

If you take 2017-05-30 & 2017-06-01 it returns 3 days - supposed to be 2 days

var oneDay = 24*60*60*1000;
var firstDate = new Date(2017, 11, 29);
var secondDate = new Date(2017, 12, 01);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
Mac
  • 334
  • 1
  • 3
  • 13
  • make a use of momentjs perhaps would make it a little easier? – MrNew Mar 29 '17 at 08:58
  • Thank you for your reply MrNew, i will defently look into momentjs in the future <3 – Mac Mar 29 '17 at 09:03
  • In the second and third examples, the dates in the text are very different to those represented by the values in the constructor, even allowing for the misinterpretation of month number. – RobG Mar 29 '17 at 11:51

3 Answers3

1

I had used 1½ hour trying to figuring out what the problem was - and 10 sec after posting i figure it out.

Problem is, the date object takes:

  • Jan, as 0
  • Feb, as 1
  • ...
  • ...
  • Nov, as 10
  • Dec, as 11
Mac
  • 334
  • 1
  • 3
  • 13
0

Please remember that Date constructor allows also values from outside the logical range.

Example: new Date(2017, -2, 30)

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Where Date is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g. new Date(2013, 13, 1) is equivalent to new Date(2014, 1, 1), both create a date for 2014-02-01 (note that the month is 0-based). Similarly for other values: new Date(2013, 2, 1, 0, 70) is equivalent to new Date(2013, 2, 1, 1, 10) which both create a date for 2013-03-01T01:10:00.

squgeim
  • 2,321
  • 1
  • 14
  • 21
kubasz
  • 1
  • 1
-1

You can calculate the difference(days) between two dates by using the code below.

var dateOne = new Date(firstDate);
var dateTwo = new Date(secondDate);
var dateDifference = Math.floor((dateTwo - dateOne) / 86400000);
console.log(dateDifference);
jaylordibe
  • 111
  • 4
  • 1
    Simply posting some code without any explanation is not an answer. – RobG Mar 29 '17 at 11:49
  • Please use the [edit] link to explain how this code works and don't just give the code, as an explanation is more likely to help future readers. See also [answer]. [source](http://stackoverflow.com/users/5244995) – Jed Fox Mar 29 '17 at 14:27