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());
Advertisement
Answer
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.