getDate () from Javascript returning NULL [closed]

-1
(function(){
    var data = new Date();
    var dia = document.querySelector('.table tbody tr > td');
    dia.textContent = data.getDate();
})();

I have this function in Javascript. It always returns NULL. Why does this occur?

    
asked by anonymous 14.02.2017 / 18:19

2 answers

1

I've simulated your code here and it's working fine. See if the selector is not wrong.

(function(){
    var data = new Date();
    var dia = document.querySelector('.table tbody tr > td');
    dia.textContent = data.getDate();
})();
<table class="table">
  <tbody>
    <tr>
      <td>data vem aqui</td>
    </tr>
  </tbody>
</table>
    
14.02.2017 / 18:31
0

I updated your function to return the Date within an element with ID, but you can use the querySelector if it is the case:

$(function() {
    var data = new Date();
    var dia = document.querySelector('#aqui');
    var mes = data.getMonth()+1; // Janeiro é 0!
    if(mes < 10) mes = '0'+mes;
    var ano = data.getFullYear();
    dia.innerHTML = data.getDate() + '/' + mes + '/' + ano;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><body><pid="aqui"></p>
  
  
  
</body>
    
14.02.2017 / 18:27