0

I am trying set up a booking page where the user can pick a arrival date and and departure date and then have a function calculate the number of nights a person is staying. I am having a vary hard time doing this.

At the moment I am using moment js and pikaday to get the users input, but then can I just get the number of days they are staying and subtract one or is this logic flawed? and is there a simple way of finding the number of days?

ReganPerkins
  • 1,645
  • 3
  • 17
  • 36
  • possible duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Andrew Hendrie Mar 10 '15 at 20:19

1 Answers1

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

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

Community
  • 1
  • 1
Andrew Hendrie
  • 6,205
  • 4
  • 40
  • 71