Format date in javascript [duplicate]

0

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

Could you help me with some function or a hint in how to handle 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>"
    ); 
};
    
asked by anonymous 06.12.2017 / 14:23

2 answers

1

If it's a "YYYY-MM-DD" string 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 both ways

    
06.12.2017 / 14:49
0

Have you tried to get the data for the date in separate variables and then put it in 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;
    
06.12.2017 / 14:30