How to print the current year on a website with JavaScript?

Spread the love

Sometimes, we want to print the current year on a website with JavaScript.

In this article, we’ll look at how to print the current year on a website with JavaScript.

How to print the current year on a website with JavaScript?

To print the current year on a website with JavaScript, we can add a text node with the current year.

For instance, we write

<div>
  &copy;
  <span id="copyright">
    <script>
      document
        .getElementById("copyright")
        .appendChild(document.createTextNode(new Date().getFullYear()));
    </script>
  </span>
  Company Name
</div>

to get the span with getElementById.

Then we call appendChild with a text node that has the current 4 digit year.

We call createTextNode to create a text node.

And we use new Date().getFullYear() to get the current 4 digit year.

Conclusion

To print the current year on a website with JavaScript, we can add a text node with the current year.

Leave a Reply

Your email address will not be published.