Formatting date and time with Javascript? [duplicate]

2

I'm consuming data from a API and would like to know how to pass the following date and time value to the Brazilian standard.

The format returned is this:

2017-01-05T14:35:17.437

but I'd like it to be displayed

05-01-2017
    
asked by anonymous 05.01.2017 / 20:12

2 answers

2

You can use momentjs that does all the work of date formatting , calculations , etc :

moment.locale('pt-br');
console.log(moment('2017-01-05T14:35:17.437').format('DD-MM-YYYY'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js"></script>

but can also be done with pure :

var data = '2017-01-05T14:35:17.437';
var parte = data.substring(0,10).split('-').reverse().join('-');
console.log(parte);

05.01.2017 / 20:22
2

Just see how to string your date and test it:

Ex:

var formatoCompleto = "2017-01-05T14:35:17.437";
var dataFormatada = formatoCompleto.substring(0,formatoCompleto.indexOf("T"));
var dadosData = dataFormatada.split("-");

var dataFinal = dadosData[2]+"-"+dadosData[1]+"-"+dadosData[0];
console.log(dataFinal);
    
05.01.2017 / 20:22