1

I', trying to get the difference (in days) between two dates generated by the jquery ui datepicker plugin, this is how i am trying (based on this acepted answer)

var sDate = $('.start input').datepicker('getDate');
var nDate = $('.end input').datepicker('getDate');

console.log(sDate); /* Logs Date {Wed Oct 16 2013 00:00:00 GMT+0200 (CEST)} */
console.log(nDate); /* Logs Date {Fri Oct 18 2013 00:00:00 GMT+0200 (CEST)} */

var dias = (nDate - sDate)/1000/60/60/24;

console.log(dias); /* Logs 30 */

Problem is that I selected two dates with 2 days of difference and this is loging 30,

what am I missing here?

Community
  • 1
  • 1
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378
  • 1
    Can you get `sDate.getTime()` and `nDate.getTime()` to check exactly what is happening? – Amadan Oct 10 '13 at 07:37
  • Already tried.. then it only returns 16 and 18, the problem i see here is when they're different months – Toni Michel Caubet Oct 10 '13 at 07:42
  • `16` and `18` seem like outputs of [`Date.getDate()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate). I'm asking about [`Date.getTime()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime), which should be a big number like `1381391208156`. – Amadan Oct 10 '13 at 07:46

1 Answers1

1

Working demo http://jsfiddle.net/xUKnX/

Also look at one of my old reply: How to add/subtract dates with javascript?

Below should fit your need :)

code

$('.start,.end').datepicker();

$('.hulk').click(function () {
    var sDate = $('.start').val();
    var nDate = $('.end').val();

    console.log(sDate); /* Logs Date {Wed Oct 16 2013 00:00:00 GMT+0200 (CEST)} */
    console.log(nDate); /* Logs Date {Wed Oct 18 2013 00:00:00 GMT+0200 (CEST)} */
    var startdate = new Date(sDate);

    var enddate = new Date(nDate);

    enddate.setDate(enddate.getDate() - startdate.getDate() -1);
    alert(enddate.getDay());
});

Screenshot from working version

enter image description here

Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77