I want to display the current date on a particular div while loading the page, I've already done a functional sketch of the code, however I'm using the button . Follow the code:
function formataData(data = new Date()){
var dia = data.getDate();
var mes = data.getMonth()+1;
var ano = data.getFullYear();
if(dia.toString().length == 1) dia = '0'+dia;
if(mes.toString().length == 1) mes = '0'+mes;
return dia+'/'+mes+'/'+ano;
}
document.getElementById("btnData").addEventListener("click", function myFunction() {
document.getElementById("localData").innerHTML = formataData()
});
<p>Exibindo data atual.</p>
<button id="btnData">Exibir Data >></button>
<div id="localData" style="width: 100px; height: 50px; border: 1px solid #000;"></div>