0

I am attempting to work out the date difference between two date objects in JavaScript. However, when I attempt to utilise the Math.round function I get NaN.

The problem appears on the this line

var deliveryDays = Math.round(Math.abs((currentDate.getTime() - basketDate.getTime())/(oneDay)));

Please see the following code:

 function datePicker() {
     $('#datePicker').datepicker();

     $('.ui-datepicker').addClass('datePicker'); //This is added to the class ui-datepicker which is generated when the date picker is generated.
     var currentDate = $.datepicker.formatDate('dd-mm-yy', new Date()); // Get today's date.
     currentDate = new Date(currentDate); // Convert string to date (string must be an appropriate format.

     $("#datePickerConfirmation").click(function () {
         var basketDate = $('#datePicker').datepicker('getDate');
         basketDate = $.datepicker.formatDate('dd-mm-yy', new Date(basketDate)); // Changes the date format to dd-mm-yyyy.
         basketDate = new Date(basketDate);

         // This block works out the difference between the current date and the selected date.
         var oneDay = 24 * 60 * 60 * 1000;
         var deliveryDays = Math.round(Math.abs((currentDate.getTime() - basketDate.getTime()) / (oneDay)));

         basketDate = basketDate.toString();
         currentDate = currentDate.toString();
         deliveryDays = deliveryDays;

         var result = window.confirm('Confirm Date: ' + basketDate + '\n \nDays to Delivery: ' + deliveryDays);
         if (result == true) {
             console.log("User pressed Ok / Confirm   Date set to: " + basketDate);
             // Do nothing
         } else {
             console.log("User pressed Cancel, basket set to null");
             basketDate = null;
             console.log(basketDate);
         }
     });
 }
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Nikolai5
  • 5
  • 2
  • Take a look [here](http://stackoverflow.com/questions/17064540/date-function-not-working-in-ie8/17064680#17064680) for details. Anyway you format a date as "dd-mm-yyyy" but this MAY not be the format Date constructor accept...just remove line where you format Date to string then convert back to Date... – Adriano Repetti Nov 20 '13 at 13:07

1 Answers1

0

You don't need to convert dates to strings and then again to dates. Replace these lines

var currentDate = $.datepicker.formatDate('dd-mm-yy', new Date()); // Get today's date.
currentDate = new Date(currentDate);

with this

var currentDate = new Date();

As well as these lines

basketDate = $.datepicker.formatDate('dd-mm-yy', new Date(basketDate)); // Changes the date format to dd-mm-yyyy.
basketDate = new Date(basketDate);

with this

basketDate = new Date(basketDate);
matewka
  • 9,912
  • 2
  • 32
  • 43
  • Yeah you're answer led me to fixing the issue. I had to remove the string conversions completely and move the dat formatting to after the Math so that its formatted just for the output to the user. – Nikolai5 Nov 20 '13 at 15:21