0

I need your help,

How can one compare the difference between two given dates while using the US date standard of mm/dd/yyyy?

A few examples below:

11/20/2014 minus 11/25/2014 = -5

11/25/2014 minus 11/25/2014 = 0

11/27/2014 minus 11/25/2014 = 2
BobbyJones
  • 1,331
  • 1
  • 26
  • 44
  • 1
    You should separate this into two tasks: 1) parsing the value from a string into a more appropriate format (e.g. `Date`); 2) comparing the two values produced by the parse step. – Jon Skeet Nov 25 '14 at 17:56
  • Possible duplicate of http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript – Paul Abbott Nov 25 '14 at 17:57

1 Answers1

1
var date1 = new Date("11/20/2014");
var date2 = new Date("11/25/2014");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 

try this

ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
  • Be careful around DST. For example, date1 = new Date("11/2/2014"), date2 = new Date("11/3/2014"); This will produce a diffDays of 2 when it should really be 1... May want to use Math.round – Patrick Nov 25 '14 at 18:09
  • @ashkufaraz how do you calculate months difference – Abdullah Al Noman Feb 19 '16 at 11:48