1

I can't seem to get the below code to work properly. While it calculates the difference in the dates, it doesnt account for the years, which throws it way off. The below code returns a difference of only 4 days and ignores the years?

How can this be fixed?

<!DOCTYPE html>

<html>

  <head>

    <script type="text/javascript">
    function test() {

      var today = "27/11/2013"
      today = new Date(today.split('/')[2],today.split('/')[1],today.split('/')[0]);

      var date2 = "23/02/2011"
      date2 = new Date(date2.split('/')[2],date2.split('/')[1],date2.split('/')[0]);

      var diff = today.getDate() - date2.getDate()
      alert(diff)
  }
  </script>

</head>

<body>
  <input type="button" value="test date" onclick="test()"/>
</body>

</html>
Mark Walters
  • 12,060
  • 6
  • 33
  • 48
Jason Kelly
  • 2,539
  • 10
  • 43
  • 80
  • 1
    possible duplicate of [Get difference between 2 dates in javascript?](http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – Felix Kling Nov 27 '13 at 16:26
  • 1
    and [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/q/542938/218196) – Felix Kling Nov 27 '13 at 16:27

4 Answers4

4

You're using the .getDate() method which returns the day of the month so you are only comparing the days of that particular month against one another. Use the getTime() method to return a millisecond representation of the date which can be used for comparison. Also remember that when using JavaScript months you need to -1 when creating a date as 0 is January and 11 is December. This goes the other way as well if constructing a Date string manually from a Date object, you'll need to remember to add 1 to the month.

Here is some code that should compare two dates and return the difference in days.

var today = "27/11/2013"
today = new Date(today.split('/')[2],today.split('/')[1]-1,today.split('/')[0]);
var date2 = "23/02/2011"
date2 = new Date(date2.split('/')[2],date2.split('/')[1]-1,date2.split('/')[0]);
var timeDiff = Math.abs(date2.getTime() - today.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays)​; //1008

You create two date objects, and compare their millisecond representations using .getTime() and then convert this back to days by dividing it back up.

Here is a fiddle

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
2

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

You're using getDate(), which returns a day of the month, that's why it's ignoring the year. Just compare Dates like this instead:

var diff = today - date2;

diff will then contain the difference in milliseconds, which you can do with what you please.

ToastyMallows
  • 4,203
  • 5
  • 43
  • 52
1

getDate() Returns the day of the month (from 1-31)

Paul Whelan
  • 16,574
  • 12
  • 50
  • 83
0

You might need to use getDate() , getMonth(), and getFullYear() individually. So maybe something like:

var day = today.getDate() - date2.getDate()
var month = today.getMonth() - date2.getMonth()
var year = today.getFullYear() - date2.getFullYear()

alert(month + "/" + day + "/" + year)