mingfish_004
mingfish_004

Reputation: 1413

how to count days between two date time in javascript?

how to count days between two date time ?
please see my codes bellow:

<script type="text/javascript">
    // how to count days between two date time ?
    var a= '2014-03-21 12:00:12';
    var b = '2014-05-11 18:00:00';
    function days_diff(a,b){
        // .....
        // ..... placeholder ....
        // ..... placeholder ....
        // ..... placeholder ....
        // ..... placeholder ....
        // ..... placeholder ....
        // .....
        // var days = days_a - days_b;
        return days;
    }

</script>   

Upvotes: 0

Views: 440

Answers (3)

rk rk
rk rk

Reputation: 324

  <script>
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
    var firstDate = new Date(2008,01,12);
    var secondDate = new Date(2008,01,22);
    var diffDays = Math.round(Math.abs((firstDate.getTime()-secondDate.getTime())/(oneDay)));
  </script>

Upvotes: 3

Mayur Gupta
Mayur Gupta

Reputation: 780

This may help you...

var Date1 = new Date (2012, 6, 12);
var Date2 = new Date (2014, 3, 16);
var Days = Math.floor((Date2.getTime() - Date1.getTime())/(1000*60*60*24));

Upvotes: 1

Abhishek
Abhishek

Reputation: 567

try this

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

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

your function

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())));

Upvotes: 1

Related Questions