JavaScript array on dates

1

I'm getting data from an API and put it inside a table.

Following are the codes I used to pull the api and put it inside the table

var botaoAdicionar = document.querySelector("#buscar-viagem");

botaoAdicionar.addEventListener("click", function() {
   var xhr = new XMLHttpRequest();

   xhr.open("GET", "API AQUI");

   xhr.addEventListener("load", function() {
       var resposta = xhr.responseText;
       console.log("ola1");
       var clientes = JSON.parse(resposta);
       console.log("ola2");
       console.log(clientes);

       for (var i =0; i < 1; i++){
           console.log("ola3");
          var clientes_1 = clientes.TRACKER[i];
          adicionaClienteNaTabelaViagem(clientes_1);
          console.log("ola4");
      }    
   });

   xhr.send();
});

In this next code is how I created the td in js and put the array in it

function adicionaClienteNaTabelaViagem(cliente) {

    var clienteTr = montaTr(cliente);
    var tabela = document.querySelector("#tabela-clientes");
    tabela.appendChild(clienteTr);
}

function montaTr(cliente) {
    var clienteTr = document.createElement("tr");
    clienteTr.classList.add("cliente");

      clienteTr.appendChild(montaTd(cliente.QTDLIB, "info-STATUS"));
      clienteTr.appendChild(montaTd(cliente.PEDCD, "info-NOME"));
      clienteTr.appendChild(montaTd(cliente.NFISCA, ".info-CPF"));
      clienteTr.appendChild(montaTd(cliente.DTAENTORC, ".info-CEPENT"));
      clienteTr.appendChild(montaTd(cliente.DESPRO, ".info-DESPRO"));
      clienteTr.appendChild(montaTd(cliente.DTAPREENT, ".info-MUNENT"));

    return clienteTr;
}

function montaTd(dado, classe) {
    var td = document.createElement("td");
    td.classList.add(classe);
    td.textContent = dado;

    return td;
}

Being that I get api dates, but not in date format.

The following is what I get from the API

{TRACKER: Array(2)}
TRACKER:Array(2)
0:
CEPENT:"57000000"
CLIRET:"NAO"
CODCAR:"000152"
CODCLI:"526460"
CODPRO:"12819009       "
CPF:"02798477425   "
DESPRO:"COLCHAO GAZIN SUPREME COMPOSTO 138X188X21CM 1PL 660120 ./PR "
DTAENT:"20170715"
DTAENTORC:"20170707"
DTAMON:"20170724"

As you can see, the last two are dates, I wanted to know if I have them as a date format, and how do I do it.

    
asked by anonymous 27.10.2017 / 16:15

1 answer

1

The most logical site for this content mapping seems to be within adicionaClienteNaTabelaViagem . So you could do something like this:

formatarData(str){
    return [str.slice(0, 4), str.slice(4, 6), str.slice(-2)].join('-'); // para formato yyyy-mm-dd
    // ou para retornar um objeto Date
    return new Date(str.slice(0, 4), Number(str.slice(4, 6)) + 1, str.slice(-2));
}

function adicionaClienteNaTabelaViagem(cliente) {
    for (var chave in cliente){
        if (chave.indexOf('DTA') == 0) cliente[chave] = formatarData(cliente[chave]);
    }
    /* 
    ou explicitamente:
    cliente.DTAENTORC = formatarData(cliente.DTAENTORC);
    */
    var clienteTr = montaTr(cliente);
    var tabela = document.querySelector("#tabela-clientes");
    tabela.appendChild(clienteTr);
}
    
27.10.2017 / 17:19