0

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>   
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
mingfish_004
  • 1,385
  • 2
  • 18
  • 26
  • 2
    Check the sidebar of related questions, this is a duplicate, of a duplicate, of another duplicated, of another one... – elclanrs Mar 21 '14 at 06:12
  • May be this link will help you. http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript – Karthik AMR Mar 21 '14 at 06:14
  • possible duplicate of [How to calculate the number of days between two dates using JavaScript?](http://stackoverflow.com/questions/2627473/how-to-calculate-the-number-of-days-between-two-dates-using-javascript) – Vinod VT Mar 21 '14 at 06:14

3 Answers3

3
  <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>
user1844933
  • 3,296
  • 2
  • 25
  • 42
rk rk
  • 314
  • 1
  • 8
1

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())));
Abhishek
  • 517
  • 3
  • 18
1

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));
Mayur Gupta
  • 762
  • 3
  • 8
  • 23