Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one create a dynamic copyright date without document.write() in JavaScript?

How can one fetch and print the current year without using document.write() in JavaScript?

This is for a typical copyright line in the footer; e.g., © 2017 Stack Overflow.

Right now I'm using the standard document.write() method.

document.write(new Date().getFullYear());
like image 976
user3731460 Avatar asked Dec 10 '22 11:12

user3731460


1 Answers

Yes sure you can use an element:

<p>© Stack Overflow <span id="year"> </span></p>

Add a link to your JS like so:

<script src="js/app.js"></script>

And within this file you can do:

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

document.getElementById("year").innerHTML = date;

Alternatively you can simply use php like so:

<?php echo date("Y"); ?>

If you are using php just make sure you change the file ext to PHP.

like image 66
Diego Ponciano Avatar answered Jan 21 '23 18:01

Diego Ponciano