Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my date an Invalid Date
    primarykey
    data
    text
    <p>I am attempting to work out the date difference between two date objects in JavaScript. However, when I attempt to utilise the <code>Math.round</code> function I get <code>NaN</code>.</p> <p>The problem appears on the this line</p> <pre><code>var deliveryDays = Math.round(Math.abs((currentDate.getTime() - basketDate.getTime())/(oneDay))); </code></pre> <p>Please see the following code:</p> <pre><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); } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload