JavaScript Answers

How to print current year in a website with JavaScript?

Spread the love

To print current year in a website with JavaScript, we call the date getFullYear method.

For instance, we write

<span id="copyright"> </span>
    .appendChild(document.createTextNode(new Date().getFullYear()));

to add a span and a script tag.

In the script tag, we call getElementById to select the span.

And then we call appendChild to append the next node that we created with createTextNode to append it as the last child of the span.

We get the 4 digit year from the current date and time with getFullYear and use that as the content of the text node.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *