views:

4796

answers:

4

How do I get the number of days between two dates in jQuery? For example, given two dates in input boxes:

<input id="first" value="1/1/2000"/>
<input id="second" value="1/1/2001"/>

<script>
  alert(datediff("day", first, second)); // what goes here?
</script>
A: 

What about using formatDate from DatePicker widget? You could use it to convert the dates in timestamp format (milliseconds since 01/01/1970) and then do a simple subtraction.

kgiannakakis
+5  A: 
function parseDate(str) {
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return (second-first)/(1000*60*60*24)
}

alert(daydiff(parseDate($('#first').val()), parseDate($('#second').val())));
Miles
Thanks Miles, works great!
Michael Haren
This method doesn't work properly if there's a daylight savings jump between the two dates. However, it seems that you can use "Math.floor" to get around this problem. Therefore, the "daydiff" function should return "Math.floor((second-first)/(1000*60*60*24));".
Steve Harrison
+1  A: 

I would go ahead and grab this small utility and in it you will find functions to this for you. Here's a short example:

     <script type="text/javascript" src="date.js"></script>
     <script type="text/javascript">
      var minutes = 1000*60;
      var hours = minutes*60;
      var days = hours*24;

      var foo_date1 = getDateFromFormat("02/10/2009", "M/d/y");
      var foo_date2 = getDateFromFormat("02/12/2009", "M/d/y");

      var diff_date = Math.round((foo_date2 - foo_date1)/days);
      alert("Diff date is: " + diff_date );
     </script>
fuentesjr
This little script is great!
RichW
+4  A: 

The easiest way to get the difference between two dates:

var diff =  Math.floor(( Date.parse(str2) - Date.parse(str1) ) / 86400000);

You get the difference days (or NaN if one or both could not be parsed). The parse date gived the result in milliseconds and to get it by day you have to divided it by 24 * 60 * 60 * 1000

If you want it divided by days, hours, minutes, seconds and milliseconds:

function dateDiff( str1, str2 ) {
    var diff = Date.parse( str2 ) - Date.parse( str1 ); 
    return isNaN( diff ) ? NaN : {
     diff : diff,
     ms : Math.floor( diff            % 1000 ),
     s  : Math.floor( diff /     1000 %   60 ),
     m  : Math.floor( diff /    60000 %   60 ),
     h  : Math.floor( diff /  3600000 %   24 ),
     d  : Math.floor( diff / 86400000        )
    };
}

Here is my refactored version of James version:

function mydiff(date1,date2,interval) {
    var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
    date1 = new Date(date1);
    date2 = new Date(date2);
    var timediff = date2 - date1;
    if (isNaN(timediff)) return NaN;
    switch (interval) {
        case "years": return date2.getFullYear() - date1.getFullYear();
        case "months": return (
            ( date2.getFullYear() * 12 + date2.getMonth() )
            -
            ( date1.getFullYear() * 12 + date1.getMonth() )
        );
        case "weeks"  : return Math.floor(timediff / week);
        case "days"   : return Math.floor(timediff / day); 
        case "hours"  : return Math.floor(timediff / hour); 
        case "minutes": return Math.floor(timediff / minute);
        case "seconds": return Math.floor(timediff / second);
        default: return undefined;
    }
}
some