How to format date for dd / mm / yyyy hh: mm format using JavaScript? [duplicate]

2

The date I have this looks like this:

  

"Fri Sep 22 2017 14:42:35 GMT-0300"

I need to format it to the following format:

  

dd / mm / yyyy hh: mm

How could I do this using JavaScript or Angular?

    
asked by anonymous 22.09.2017 / 20:29

1 answer

5

I needed this time, so I did the basics:

<script>
var data = new Date("Fri Sep 22 2017 14:42:35 GMT-0300");
var dia = data.getDate(); dia = zeroAEsquerda(dia, 2);
var mescru = data.getMonth();var mes = data.getMonth(); mes += 1; mes = zeroAEsquerda(mes, 2);
var ano = data.getFullYear();
var dataAtual = dia+'/'+mes+'/'+ano;
var horaAtual       = data.getHours();          // 0-23
var minutoAtual     = data.getMinutes();        // 0-59
var segundoAtual    = data.getSeconds();        // 0-59
console.log('Data: '+dia+'/'+mes+'/'+ano+' - '+horaAtual+':'+minutoAtual+':'+segundoAtual);
function zeroAEsquerda(str, length) {
  const resto = length - String(str).length;
  return '0'.repeat(resto > 0 ? resto : '0') + str;
}
</script>
    
22.09.2017 / 20:40