How to check if a date is a holiday












0














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;
}









share|improve this question


















  • 1




    Possible duplicate of How do I return the response from an asynchronous call?
    – Patrick Hund
    Nov 20 '18 at 17:24






  • 2




    $.getJSON is asynchronous, meaning the holidays array is filled after the code that iterates it is executed. See duplicate question, this is a very common issue
    – Patrick Hund
    Nov 20 '18 at 17:25










  • Try loading the full array with the holidays as soon as the page loads, using document.onload, for exemple.
    – Pedro Lima
    Nov 20 '18 at 17:30










  • You are padding the day but not the month, so you will get a date like "01/1/2019". Also, you're setting the value of holiday, but returning feriado.
    – RobG
    Nov 20 '18 at 20:16


















0














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;
}









share|improve this question


















  • 1




    Possible duplicate of How do I return the response from an asynchronous call?
    – Patrick Hund
    Nov 20 '18 at 17:24






  • 2




    $.getJSON is asynchronous, meaning the holidays array is filled after the code that iterates it is executed. See duplicate question, this is a very common issue
    – Patrick Hund
    Nov 20 '18 at 17:25










  • Try loading the full array with the holidays as soon as the page loads, using document.onload, for exemple.
    – Pedro Lima
    Nov 20 '18 at 17:30










  • You are padding the day but not the month, so you will get a date like "01/1/2019". Also, you're setting the value of holiday, but returning feriado.
    – RobG
    Nov 20 '18 at 20:16
















0












0








0







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;
}









share|improve this question













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;
}






javascript json date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 17:22









dgo.carvalho

1




1








  • 1




    Possible duplicate of How do I return the response from an asynchronous call?
    – Patrick Hund
    Nov 20 '18 at 17:24






  • 2




    $.getJSON is asynchronous, meaning the holidays array is filled after the code that iterates it is executed. See duplicate question, this is a very common issue
    – Patrick Hund
    Nov 20 '18 at 17:25










  • Try loading the full array with the holidays as soon as the page loads, using document.onload, for exemple.
    – Pedro Lima
    Nov 20 '18 at 17:30










  • You are padding the day but not the month, so you will get a date like "01/1/2019". Also, you're setting the value of holiday, but returning feriado.
    – RobG
    Nov 20 '18 at 20:16
















  • 1




    Possible duplicate of How do I return the response from an asynchronous call?
    – Patrick Hund
    Nov 20 '18 at 17:24






  • 2




    $.getJSON is asynchronous, meaning the holidays array is filled after the code that iterates it is executed. See duplicate question, this is a very common issue
    – Patrick Hund
    Nov 20 '18 at 17:25










  • Try loading the full array with the holidays as soon as the page loads, using document.onload, for exemple.
    – Pedro Lima
    Nov 20 '18 at 17:30










  • You are padding the day but not the month, so you will get a date like "01/1/2019". Also, you're setting the value of holiday, but returning feriado.
    – RobG
    Nov 20 '18 at 20:16










1




1




Possible duplicate of How do I return the response from an asynchronous call?
– Patrick Hund
Nov 20 '18 at 17:24




Possible duplicate of How do I return the response from an asynchronous call?
– Patrick Hund
Nov 20 '18 at 17:24




2




2




$.getJSON is asynchronous, meaning the holidays array is filled after the code that iterates it is executed. See duplicate question, this is a very common issue
– Patrick Hund
Nov 20 '18 at 17:25




$.getJSON is asynchronous, meaning the holidays array is filled after the code that iterates it is executed. See duplicate question, this is a very common issue
– Patrick Hund
Nov 20 '18 at 17:25












Try loading the full array with the holidays as soon as the page loads, using document.onload, for exemple.
– Pedro Lima
Nov 20 '18 at 17:30




Try loading the full array with the holidays as soon as the page loads, using document.onload, for exemple.
– Pedro Lima
Nov 20 '18 at 17:30












You are padding the day but not the month, so you will get a date like "01/1/2019". Also, you're setting the value of holiday, but returning feriado.
– RobG
Nov 20 '18 at 20:16






You are padding the day but not the month, so you will get a date like "01/1/2019". Also, you're setting the value of holiday, but returning feriado.
– RobG
Nov 20 '18 at 20:16














2 Answers
2






active

oldest

votes


















0














Try the following code instead - notice the "$.getJSON(url).then(function (data)) {}" instead of your original code.



As someone commented, it appears your issue is due to you using the response before there is an actual response. as $.getJSON is asynchronous.



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).then(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;
}





share|improve this answer





















  • It still returned false...I tried a date that is a holiday here (October 12th) and it returned false.
    – dgo.carvalho
    Nov 20 '18 at 17:52










  • This should throw a reference error: can't find feriado.
    – RobG
    Nov 20 '18 at 20:23





















0














There are a few issues with your code:




  1. The call is asynchronous and you aren't waiting for the response

  2. You're formatting dates as DD/M/YYYY, so the comparison will fail for single digit months

  3. You haven't declared or initialised feriado so you should get a reference error


Here's a quick example testing if new year's day is a holiday:






var req = new XMLHttpRequest();
req.onload = function(e) {
var text = req.response;
var data = JSON.parse(text);
var holidays = data.map(hol => ({day:hol.date, name:hol.name}));
var nyd = '01/01/2018';
console.log(`Is ${nyd} a holiday? ${holidays.some(hol => hol.day == nyd)}`);
}

var url = 'https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3'
req.open('GET', url); // async request
req.responseType = 'text';
req.send();





PS



You should only do the request once and store the result as running it on every call to verifyHoliday is very inefficient. It's likely that holidays don't change very often (they're usually gazetted years in advance) so even once per day is more than enough.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398302%2fhow-to-check-if-a-date-is-a-holiday%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try the following code instead - notice the "$.getJSON(url).then(function (data)) {}" instead of your original code.



    As someone commented, it appears your issue is due to you using the response before there is an actual response. as $.getJSON is asynchronous.



    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).then(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;
    }





    share|improve this answer





















    • It still returned false...I tried a date that is a holiday here (October 12th) and it returned false.
      – dgo.carvalho
      Nov 20 '18 at 17:52










    • This should throw a reference error: can't find feriado.
      – RobG
      Nov 20 '18 at 20:23


















    0














    Try the following code instead - notice the "$.getJSON(url).then(function (data)) {}" instead of your original code.



    As someone commented, it appears your issue is due to you using the response before there is an actual response. as $.getJSON is asynchronous.



    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).then(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;
    }





    share|improve this answer





















    • It still returned false...I tried a date that is a holiday here (October 12th) and it returned false.
      – dgo.carvalho
      Nov 20 '18 at 17:52










    • This should throw a reference error: can't find feriado.
      – RobG
      Nov 20 '18 at 20:23
















    0












    0








    0






    Try the following code instead - notice the "$.getJSON(url).then(function (data)) {}" instead of your original code.



    As someone commented, it appears your issue is due to you using the response before there is an actual response. as $.getJSON is asynchronous.



    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).then(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;
    }





    share|improve this answer












    Try the following code instead - notice the "$.getJSON(url).then(function (data)) {}" instead of your original code.



    As someone commented, it appears your issue is due to you using the response before there is an actual response. as $.getJSON is asynchronous.



    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).then(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;
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 17:37









    Mikkel

    8321824




    8321824












    • It still returned false...I tried a date that is a holiday here (October 12th) and it returned false.
      – dgo.carvalho
      Nov 20 '18 at 17:52










    • This should throw a reference error: can't find feriado.
      – RobG
      Nov 20 '18 at 20:23




















    • It still returned false...I tried a date that is a holiday here (October 12th) and it returned false.
      – dgo.carvalho
      Nov 20 '18 at 17:52










    • This should throw a reference error: can't find feriado.
      – RobG
      Nov 20 '18 at 20:23


















    It still returned false...I tried a date that is a holiday here (October 12th) and it returned false.
    – dgo.carvalho
    Nov 20 '18 at 17:52




    It still returned false...I tried a date that is a holiday here (October 12th) and it returned false.
    – dgo.carvalho
    Nov 20 '18 at 17:52












    This should throw a reference error: can't find feriado.
    – RobG
    Nov 20 '18 at 20:23






    This should throw a reference error: can't find feriado.
    – RobG
    Nov 20 '18 at 20:23















    0














    There are a few issues with your code:




    1. The call is asynchronous and you aren't waiting for the response

    2. You're formatting dates as DD/M/YYYY, so the comparison will fail for single digit months

    3. You haven't declared or initialised feriado so you should get a reference error


    Here's a quick example testing if new year's day is a holiday:






    var req = new XMLHttpRequest();
    req.onload = function(e) {
    var text = req.response;
    var data = JSON.parse(text);
    var holidays = data.map(hol => ({day:hol.date, name:hol.name}));
    var nyd = '01/01/2018';
    console.log(`Is ${nyd} a holiday? ${holidays.some(hol => hol.day == nyd)}`);
    }

    var url = 'https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3'
    req.open('GET', url); // async request
    req.responseType = 'text';
    req.send();





    PS



    You should only do the request once and store the result as running it on every call to verifyHoliday is very inefficient. It's likely that holidays don't change very often (they're usually gazetted years in advance) so even once per day is more than enough.






    share|improve this answer




























      0














      There are a few issues with your code:




      1. The call is asynchronous and you aren't waiting for the response

      2. You're formatting dates as DD/M/YYYY, so the comparison will fail for single digit months

      3. You haven't declared or initialised feriado so you should get a reference error


      Here's a quick example testing if new year's day is a holiday:






      var req = new XMLHttpRequest();
      req.onload = function(e) {
      var text = req.response;
      var data = JSON.parse(text);
      var holidays = data.map(hol => ({day:hol.date, name:hol.name}));
      var nyd = '01/01/2018';
      console.log(`Is ${nyd} a holiday? ${holidays.some(hol => hol.day == nyd)}`);
      }

      var url = 'https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3'
      req.open('GET', url); // async request
      req.responseType = 'text';
      req.send();





      PS



      You should only do the request once and store the result as running it on every call to verifyHoliday is very inefficient. It's likely that holidays don't change very often (they're usually gazetted years in advance) so even once per day is more than enough.






      share|improve this answer


























        0












        0








        0






        There are a few issues with your code:




        1. The call is asynchronous and you aren't waiting for the response

        2. You're formatting dates as DD/M/YYYY, so the comparison will fail for single digit months

        3. You haven't declared or initialised feriado so you should get a reference error


        Here's a quick example testing if new year's day is a holiday:






        var req = new XMLHttpRequest();
        req.onload = function(e) {
        var text = req.response;
        var data = JSON.parse(text);
        var holidays = data.map(hol => ({day:hol.date, name:hol.name}));
        var nyd = '01/01/2018';
        console.log(`Is ${nyd} a holiday? ${holidays.some(hol => hol.day == nyd)}`);
        }

        var url = 'https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3'
        req.open('GET', url); // async request
        req.responseType = 'text';
        req.send();





        PS



        You should only do the request once and store the result as running it on every call to verifyHoliday is very inefficient. It's likely that holidays don't change very often (they're usually gazetted years in advance) so even once per day is more than enough.






        share|improve this answer














        There are a few issues with your code:




        1. The call is asynchronous and you aren't waiting for the response

        2. You're formatting dates as DD/M/YYYY, so the comparison will fail for single digit months

        3. You haven't declared or initialised feriado so you should get a reference error


        Here's a quick example testing if new year's day is a holiday:






        var req = new XMLHttpRequest();
        req.onload = function(e) {
        var text = req.response;
        var data = JSON.parse(text);
        var holidays = data.map(hol => ({day:hol.date, name:hol.name}));
        var nyd = '01/01/2018';
        console.log(`Is ${nyd} a holiday? ${holidays.some(hol => hol.day == nyd)}`);
        }

        var url = 'https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3'
        req.open('GET', url); // async request
        req.responseType = 'text';
        req.send();





        PS



        You should only do the request once and store the result as running it on every call to verifyHoliday is very inefficient. It's likely that holidays don't change very often (they're usually gazetted years in advance) so even once per day is more than enough.






        var req = new XMLHttpRequest();
        req.onload = function(e) {
        var text = req.response;
        var data = JSON.parse(text);
        var holidays = data.map(hol => ({day:hol.date, name:hol.name}));
        var nyd = '01/01/2018';
        console.log(`Is ${nyd} a holiday? ${holidays.some(hol => hol.day == nyd)}`);
        }

        var url = 'https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3'
        req.open('GET', url); // async request
        req.responseType = 'text';
        req.send();





        var req = new XMLHttpRequest();
        req.onload = function(e) {
        var text = req.response;
        var data = JSON.parse(text);
        var holidays = data.map(hol => ({day:hol.date, name:hol.name}));
        var nyd = '01/01/2018';
        console.log(`Is ${nyd} a holiday? ${holidays.some(hol => hol.day == nyd)}`);
        }

        var url = 'https://api.calendario.com.br/?json=true&ano=2018&estado=SP&cidade=MOGI_GUACU&token=ZGdvLmRpZWdvY2FydmFsaG9AZ21haWwuY29tJmhhc2g9MTYzMjcxMDY3'
        req.open('GET', url); // async request
        req.responseType = 'text';
        req.send();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 22:17

























        answered Nov 20 '18 at 20:47









        RobG

        97k19103145




        97k19103145






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398302%2fhow-to-check-if-a-date-is-a-holiday%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin