Number of days between two dates using javascript

I get the date from the datetime collector with this:

var endDate = new Date();
endDate = $("input[id$='DateTimeControl2Date']").val()

Then I find a different date and try to check how many days between the two dates, but I get the error "The object does not support this property or method." What am I doing wrong?

$('.StatusDateTable').each(function() {
var statusDate = new Date(); 
statusDate = $(this).find(".dates").html();
var statusLight = $(this).find(".StatusLight").attr("src");
statusLight = statusLight.substring(33).slice(0,-9);
if (statusLight == "Blue") {
var oneDay = 1000*60*60*24;

alert(endDate + statusDate);
var date1_ms = endDate.getTime();
var date2_ms = statusDate.getTime();

var dayDifference = Math.abs(Math.round((date1_ms - date2_ms)/oneDay));

alert(dayDifference);

}
});

endDate has the format 02/09/2010 and statusDate 1/09/2010.

Thanks in advance.

+3
source share
2 answers

Date () is a constructor function, when you call it, you assign the result of this constructor to a variable. Then in the line after that, you assign another object, the result of the jQuery val () method, to the same variable.

// The next line will overwrite the current value of `endDate`
endDate = $("input[id$='DateTimeControl2Date']").val()

Date() , . /:

var endDate = new Date($("input[id$='DateTimeControl2Date']").val());

, , Date(). statusDate.

</" > , Date(). IE, , mm/dd/yyyy. , Date():

var endDateSplit = $("input[id$='DateTimeControl2Date']").val().split("/"),
    endDate = new Date(endDateSplit[2], endDateSplit[1]-1, endDateSplit[0]);
+3
0

Source: https://habr.com/ru/post/1763038/


All Articles