Change date format in javaScript?

0

My date format is yyyy-mm-dd and I want to change to dd-mm-yyyy

I'm pulling the date data from an api, so I put it with the "-" example: 2017-12-31 because before it only came with 20171231 but I want to change to 31-12-2017

First I pull the API to my site:

function load() {
  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];
         adicionaClienteNaTabelaEntregue(clientes_1);
         console.log("ola4");
     }

  });

  xhr.send();
      }
      window.onload = load;

Then I put it on my table

    function adicionaClienteNaTabelaEntregue(cliente) {
//    var pacienteTr = montaTr(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.DTAENT, "info-entconfirmada"));
      clienteTr.appendChild(montaTd(cliente.DTAMONORC, ".info-montprevista"));
      clienteTr.appendChild(montaTd(cliente.DTAMON, ".info-montconfirmada"));
      clienteTr.appendChild(montaTd(cliente.FILORC, "info-loja"));

    return clienteTr;
}

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

    return td;
}

And the API data looks like this:

DTAMON:"2017-07-24"
DTAMONORC:"2017-07-12"
DTAPREENT:"2017-07-14"
    
asked by anonymous 30.10.2017 / 16:20

1 answer

1

Perhaps the fastest and perhaps not very secure way (if the format is always in this standard, can be used without problems), but, functional would be:

var _str = '2016-03-01';
_new = _str.split('-').reverse().join('-');
console.log(_new);

split will break this text into 3 parts of array , the reverse command will change the order where the first one will be the last one the second the third one and so on and join resets the date in the desired format, joining all positions with the informed tab.

In your current code create a function as follows:

function formatDate(str)
{
    return str.split('-').reverse().join('-');
}

and in your code call:

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

    clienteTr.appendChild(montaTd(formatDate(cliente.DTAENT), "info-entconfirmada"));
    clienteTr.appendChild(montaTd(formatDate(cliente.DTAMONORC), ".info-montprevista"));
    clienteTr.appendChild(montaTd(formatDate(cliente.DTAMON), ".info-montconfirmada"));
    clienteTr.appendChild(montaTd(cliente.FILORC, "info-loja"));

    return clienteTr;
}

References

30.10.2017 / 16:34