How to show the current date when the page loads

1

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>      
    
asked by anonymous 22.07.2018 / 20:43

3 answers

1

Just put the onload event in the role and remove the onclick event from the button.

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;
} 

window.onload = function(){
   document.getElementById("localData").innerHTML = formataData();
};
<p>Exibindo data atual.</p>

<div id="localData" style="width: 100px; height: 50px; border: 1px solid #000;"></div>
    
22.07.2018 / 21:14
1

Easy-to-configure script

  

With this script you can set the month as it pleases you (Jan, January, etc.).

window.onload = function(){
    var d=new Date();
    var mes=new Array("01","02","03","04","05","06","07","08","09","10","11","12");
    var DataHoje = d.getDate() + "/" + mes[d.getMonth()] + "/" + d.getFullYear();
    document.getElementById("DataAtual").innerHTML = DataHoje;
}
<div id="DataAtual"></div>
  

If you want to enter the name of the day of the week (Monday, Monday, etc.)

var d=new Date();
var dia_semana=new Array("Domingo","Segunda","Terça","Quarta","Quinta","Sexta","Sabado");
var mes=new Array("01","02","03","04","05","06","07","08","09","10","11","12");
var HOJE = dia_semana[d.getDay()] + ", " + d.getDate() + "/" + mes[d.getMonth()] + "/" + d.getFullYear();
document.getElementById("DataAtual").innerHTML = HOJE;
<div id="DataAtual"></div>
    
23.07.2018 / 01:56
0

Place an onload on <body>

See the code below

<!DOCTYPE html>
<html>
<body onload="mostrarHora()">


 <p>Exibindo data atual.</p>



    <div id="localData" style="width: 100px; height: 50px; border: 1px solid #000;"></div>

<script>
    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;
    } 


    function mostrarHora() {
    document.getElementById("localData").innerHTML = formataData()};
    </script>


</body>
</html>
    
22.07.2018 / 21:10