Change date format in Java?

Asked

Viewed 2,176 times

0

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

I’m pulling the 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 website:

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 comes like this:

DTAMON:"2017-07-24"
DTAMONORC:"2017-07-12"
DTAPREENT:"2017-07-14"

1 answer

3


Perhaps the fastest and perhaps not very safe way (if the format is always in this pattern, can be used smoothly), but, functional would be:

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

the split will break this text into 3 parts of a array, the command re-verse will change the order where the first will be the last the second the last and so on and Join reassemble 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

  • But I’m not getting a date, the date comes from the api and it can change, you know?

  • 1

    His code was just an example @Nazarelisboa, instead of the first line, you spend your date in the format you currently have.

  • @Nazarelisboa the example of both my and the other friend who answers in a way satisfies his doubt, but, your question we do not know where the data comes from, could edit and inform this to us!?

  • Ready. I’ve already edited the question

  • @Nazarelisboa does a function and then call in the method that mounts the table, I made the edition is in the answer.

  • 1

    You solved my problem. Thank you very much!

Show 1 more comment

Browser other questions tagged

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