Javascript array in dates

Asked

Viewed 119 times

1

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

The following are the codes I used to pull the api and put 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 the dates api, but are not in date format.

Next 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 was wondering if there’s any way I can turn them into date format and how do I do that.

1 answer

1


The most logical place 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);
}
  • That’s right! Thank you very much!!!

Browser other questions tagged

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