How do I get the number of days between two dates in JavaScript

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

How do I get the number of days between two dates in JavaScript? 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>

Answers

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

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

alert(daydiff(parseDate($( #first ).val()), parseDate($( #second ).val())));

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils