I have this situation, a form where the user sets the date and a number of days so I can calculate a dead line date via javascript methods. I found an api that return json objects with the holidays in my city. the problem is, when I call my method passing a date that I know that is a holiday and it is present in the json object, the method returns false. If I copy and paste the method line by line on chrome's dev tool, it return true. what am I missing?
OBS: the date format used in my country is dd/mm/YYYY, that's why I use some date formatting and to compare with the dates returned in the json objects.
function verifyHoliday(date){
var url = "https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3";
var day = (date.getDate() < 10) ? "0"+date.getDate() : date.getDate();
var dateString = day + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
//====================================================
var holiday = false;
var holidays = [];
$.getJSON(url, function(data){
for(var i = 0; i < data.length; i++){
var obj = {day: data[i].date, name: data[i].name};
holidays[i] = obj;
}
});
for(var i = 0; i < holidays.length; i++){
if(holidays[i].day == dateString){
holiday = true;
break;
}
}
return feriado;
}