Date Conversion

5

I have a date in this format

"2017-10-13T18:15:41.143Z"

And would like a date in this format

10/06/2017 01:42:34 PM (-0300)

How can I do the conversion?

    
asked by anonymous 13.10.2017 / 20:20

1 answer

4

I created a formatting function, basically I unmasked the date and was concatenating gradually.

Concatenate the left zero in a few moments to make sure it has 2 digits.

At times I make a calculation and check if it is greater than or equal to 12 to indicate whether it is PM or AM.

I use the getTimezoneOffset () function that returns the difference in minutes of the time zone.

function formatDate(date) {
  var dia       = ("00" + date.getDate()).slice(-2);
  var mes       = date.getMonth()+1;
  var ano       = date.getFullYear();
  var minutos   = ("00" + date.getMinutes()).slice(-2);
  var segundos  = ("00" + date.getSeconds()).slice(-2);
  var timezone  = date.getTimezoneOffset();
  var horas     = date.getHours() % 12 ? date.getHours() % 12 : 12;
  
  horas = ("00" + horas).slice(-2);
  var txtHoras = horas >= 12 ? 'PM' : 'AM';
  
  var retorno;
  retorno = dia + '/' + mes + '/' + ano + ' ';
  retorno = retorno + horas + ':' + minutos + ':' + segundos;
  retorno = retorno + ' ' + txtHoras + ' (' + timezone + ')';
  return  retorno;
}

console.log(new Date()); 
console.log(formatDate(new Date())); 
    
13.10.2017 / 20:41