Format date in javascript


  • QA Engineer

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

    Could you help me with some function or a tip 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>"
        ); 
    };
    


  • If it is a string "YYYY-MM-DD" and you want to get another string "D-MM-YYYY" 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




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2