1

I'm trying to print the current year next to my copyright in my footer, but it's not working.
The "Name " will be replaced by my brand name.

<div class="footer-content-3">
    <i class="far fa-copyright"></i>
    <h4 id="copyright-year"></h4>
</div>

<script>
    var year = getFullYear();
    document.getElementById('copyright-year').innerHTML = ("Name " + year);
</script>
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Jon Nicholson
  • 927
  • 1
  • 8
  • 29
  • 2
    Does this answer your question? [Get the current year in JavaScript](https://stackoverflow.com/questions/6002254/get-the-current-year-in-javascript) – Shoejep May 21 '20 at 10:26
  • `getFullYear()` is not a global method, but a method of a `Date` object. So... `var year = (new Date()).getFullYear()`. However it would be much better to have the copyright in the real HTML source, not just on screen. – pawel May 21 '20 at 10:27

3 Answers3

5

You must use date before use getfullyear

var date = new Date();
var year = date.getFullYear();

document.getElementById('copyright-year').innerHTML = ("Name " + year);
<div class="footer-content-3">
  <i class="far fa-copyright"></i>
  <h4 id="copyright-year"></h4>
</div>
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
2

Use:

new Date().getFullYear()

in the place of your:

getFullYear()

to get the correct full year.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
1

You must have to use Date object:

var today = new Date();
var year = today.getFullYear();
Bogdan
  • 51
  • 4