Get national holidays

Asked

Viewed 11,938 times

14

I am making an application and would like to know if there is any way to get the national holidays.

For example: Some Google Calendar API where I can get some XML to consume..

  • By webservice I know the Multi-table.

  • Answer. Put your comment as an answer that I already accept.

  • The Multitables has a PHP Webservice. It is possible to obtain not only national holidays, but also regional holidays.

  • To Elekto (warning: it is my company) provides a JSONP API, free to use, explained in detail here. It is the API that is used in our Deadline tool in working days. The focus, however, is the financial market. The tool has been on the air since 2012-05, hosted in São Paulo (less latency).

  • The big problem in Brazil are the municipal holidays and the number of existing cities. I just found an API that has holidays from every county. I am using and it has worked well: http://www.calendario.com.br/api_feriados_municipais_estaduais_nacionais.php. It is free, just put the email on this page.

  • In Portugal but without the municipalities. https://developers.blogs.sapo.pt/3744.html

Show 1 more comment

2 answers

13

You can use the http://holidayapi.com which is an API that supports the following countries/regions BE, BR, GB, NO or US (note that you use the ISO 3166-1 alpha-2 which is version alpha).

Just make a request to the address http://holidayapi.com/v1/holidays which can be used with any language, C# should look something like:

WebRequest request = WebRequest.Create("http://holidayapi.com/v1/holidays?country=BR&year=2015");

request.Credentials = CredentialCache.DefaultCredentials;

((HttpWebRequest) request).UserAgent = context.Request.UserAgent;

HttpWebResponse response = (HttpWebResponse) request.GetResponse();

if (response.StatusCode != HttpStatusCode.OK) {
    response.StatusCode.ToString();
} else {
    Stream receiveStream = response.GetResponseStream();

    //Converte a resposta para objeto
}

Or you can download from Github: https://github.com/joshtronic/holidayapi.com (see the folder https://github.com/joshtronic/holidayapi.com/tree/master/data)

As an example (unfortunately the repository is only for PHP, but you can use the files .json):

<?php
require '../lib/HolidayAPIv1.php';
$api = new \HolidayAPI\v1($redis);
header('Content-Type: application/json; charset=utf-8');
$flags = JSON_UNESCAPED_UNICODE;
if (isset($_REQUEST['pretty'])) {
    $flags |= JSON_PRETTY_PRINT;
}
echo json_encode($api->getHolidays(), $flags);
  • Holidays to Portugal are wrong

  • @Sandromarques is very vague, of details of how it used, type Pastebin.

4

No API, only math calculation and library Moment js.

function easterDay(y) {
    var c = Math.floor(y / 100);
    var n = y - 19 * Math.floor(y / 19);
    var k = Math.floor((c - 17) / 25);
    var i = c - Math.floor(c / 4) - Math.floor((c - k) / 3) + 19 * n + 15;
    i = i - 30 * Math.floor((i / 30));
    i = i - Math.floor(i / 28) * (1 - Math.floor(i / 28) * Math.floor(29 / (i + 1)) * Math.floor((21 - n) / 11));
    var j = y + Math.floor(y / 4) + i + 2 - c + Math.floor(c / 4);
    j = j - 7 * Math.floor(j / 7);
    var l = i - j;
    var m = 3 + Math.floor((l + 40) / 44);
    var d = l + 28 - 31 * Math.floor(m / 4);
    return moment([y, (m - 1), d]);
};

function getHolidaysBr(y) {
    var anoNovo = moment("01/01/"+y,"DD/MM/YYYY");
    var carnaval1 = easterDay(y).add(-48, "d");
    var carnaval2 = easterDay(y).add(-47, "d");
    var paixaoCristo = easterDay(y).add(-2, "d");
    var pascoa = easterDay(y);
    var tiradentes = moment("21/04/"+y,"DD/MM/YYYY");
    var corpusChristi =  easterDay(y).add(60, "d");
    var diaTrabalho = moment("01/05/"+y,"DD/MM/YYYY");
    var diaIndependencia = moment("07/09/"+y,"DD/MM/YYYY");
    var nossaSenhora = moment("12/10/"+y,"DD/MM/YYYY");
    var finados = moment("02/11/"+y,"DD/MM/YYYY");
    var proclamaRepublica = moment("15/11/"+y,"DD/MM/YYYY");
    var natal = moment("25/12/"+y,"DD/MM/YYYY");
    return [
        {m: anoNovo, dia: "Ano Novo", d: anoNovo.format("DD/MM/YYYY") },
        {m: carnaval1, dia: "Carnaval", d: carnaval1.format("DD/MM/YYYY") },
        {m: carnaval2, dia: "Carnaval", d: carnaval2.format("DD/MM/YYYY") },
        {m: paixaoCristo, dia: "Paix\u00E3o de Cristo", d: paixaoCristo.format("DD/MM/YYYY") },
        {m: pascoa, dia: "P\u00E1scoa", d: pascoa.format("DD/MM/YYYY") },
        {m: tiradentes, dia: "Tiradentes", d: tiradentes.format("DD/MM/YYYY") },
        {m: corpusChristi, dia: "Corpus Christi", d: corpusChristi.format("DD/MM/YYYY") },
        {m: diaTrabalho, dia: "Dia do Trabalho", d: diaTrabalho.format("DD/MM/YYYY") },
        {m: diaIndependencia, dia: "Dia da Independ\u00EAncia do Brasil", d: diaIndependencia.format("DD/MM/YYYY") },
        {m: nossaSenhora, dia: "Nossa Senhora Aparecida", d: nossaSenhora.format("DD/MM/YYYY") },
        {m: finados, dia: "Finados", d: finados.format("DD/MM/YYYY") },
        {m: proclamaRepublica, dia: "Proclama\u00E7\u00E3o da Rep\u00FAblica", d: proclamaRepublica.format("DD/MM/YYYY") },
        {m: natal, dia: "Natal", d: natal.format("DD/MM/YYYY") }
    ];
}
function getHolidayBetweenDates(date, dateTo){
    var dateStart = moment(date,"YYYY-MM-DD");
    var dateEnd = moment(dateTo,"YYYY-MM-DD");
    var datesHoliday = [];

    while (dateEnd > dateStart || dateStart.format("Y") === dateEnd.format("Y")) {
       $.merge(datesHoliday,getHolidaysBr(parseInt(dateStart.format("YYYY"))));
       dateStart.add(1,"year");
    }
    return datesHoliday;
}

var hollidays = getHolidayBetweenDates("2016-01-01","2020-01-01");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.