I have an array which i need to pass over to a php page, the array is fine, but i have to include a new variable into it, variable is lastBookingDate, then this variable i match with current date and get the month diff so far is good, the issue is when the varible is undefined i got the error
Uncaught TypeError: Cannot read property 'split' of undefined at HTMLDocument. (:2:170)"
I have an if statement but I keep getting the same error. The variable info is coming from a variable in datalayer(GTM) that when there no info the variable doesn't exist at all
var bookingDateExist = GTM;
if (bookingDateExist === "undefined") {
var Booking = "no booking";
} else {
var lastBookedDate = bookingDateExist;
var dateString = lastBookedDate;
var dateParts = dateString.split("/");
var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
var start_date = new Date(); //Create start date object by passing appropiate argument
var end_date = dateObject;
var total_months = (end_date.getFullYear() - start_date.getFullYear()) * 12 + (end_date.getMonth() - start_date.getMonth());
var Booking = Math.abs(total_months);
}
I expected variable Booking = no booking or variable booking = 12 (amount of month diff)