Format date in javascript

Asked

Viewed 2,097 times

0

The code has an array that receives the information from DB. The date field receives the date in YYYY-MM-DD format, but I would like to display it in the following order DD-MM-YYYY.

Could you help me with some function or a hint on how to treat this problem?

for(var i=0;dados.length>i;i++){
    $('#table').append(  
        "<td align = 'left' style='width:40px;max-width:40px;'<b>"+dados[i].Conta+"</b> </td>"+  
        "<td align = 'left' style='width:50px;max-width:50px;'><b>"+dados[i].codigoAcesso+"</b> </td>"+  
        "<td align = 'left' style='width:50px;max-width:50px;'><b>"+dados[i].nome+"</b> </td>"+  
        "<td align = 'left' style='width:50px;max-width:50px;'><b>"+dados[i].data+"</b> </td>"+  
        "</table>"
    ); 
};
  • Thanks Caique! Although I deal with the same subject, my biggest difficulty is to take the date of the array and format it. That’s why I chose to create a new question.

  • Take the array date is do dados[i].data, interpreting and formatting it is exactly what appears in the duplicate indicated by @Caiqueromero. It has N solutions, both manual and library

2 answers

1

If it is a string "YYYY-MM-DD" and you want to get another string " DD-MM-YYYY" you can do so

"YYYY-MM-DD".split("-").reduce(function(p, c){ return c + "-" +p })

or create a function for this purpose

function f(str){ return str.split("-").reduce(function(p, c){ return c + "-" +p })}

and convert in both directions inserir a descrição da imagem aqui

0

Have you ever tried to get date data into separate variables and then merge it into a new variable? It would look like this:

var data=new Date()
var dia=data.getDate();
var mes=data.getMonth();
var ano=data.getFullYear();
data = dia + '/' + (mes++) + '/' + ano;
  • Okay, I’m gonna take this test and I’ll be right back!

Browser other questions tagged

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