function dataFormatada(dataBanco){
let data = new Date(dataBanco),
dia = data.getDate().toString(),
diaF = (dia.length == 1) ? '0'+dia : dia,
mes = (data.getMonth()+1).toString(), //+1 pois no getMonth Janeiro começa com zero.
mesF = (mes.length == 1) ? '0'+mes : mes,
anoF = data.getFullYear();
// return diaF+"/"+mesF+"/"+anoF;
console.log(diaF+"/"+mesF+"/"+anoF);
}
/* ###### data vindo do banco exemplo com PHP
dataBanco="<?php echo $dataBanco ?>";
########################################### */
dataBanco="2020-07-06 15:00:00";
//chamada da função
dataFormatada(dataBanco);
The task of instruction new Date( )
is to create a memory location for all the data that a date needs to store.
console.log(new Date("2020-07-06 15:00:00"));
The method getDate()
returns the day of the month
The method toString()
converts a Date object into a string
let data = new Date("2020-07-06 15:00:00")
console.log(data.getDate().toString());
getMonth()
- month in year (January = 0)
getFullYear()
- returns the year of the specified date according to local time
With this data you build your date the way you want it
(diaF+"/"+mesF+"/"+anoF)
(anoF+"/"+mesF+"/"+diaF)