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

5

8

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());
Algolagnia answered 1/5, 2017 at 3:46 Comment(4)
document.getElementById("someElementId").innerHTML = new Date().getFullYear();? (The date/copyright part of this is irrelevant, right? As in, you just need to be able to display any value that comes from JS?)Nourishment
That set me on the right path. Thanks!Algolagnia
What's wrong with using document.write (other than depending on the client to be set to the correct year)?Murat
@Murat Lighthouse returned error: [Violation] Avoid using document.write(). because it adversely affects performance.Rufena
D
13

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.

Deduct answered 1/5, 2017 at 4:2 Comment(1)
For beginners if you use PHP you need a PHP Server too :)Deduct
A
6

document.getElementById("current-year").innerHTML = new Date().getFullYear();
<span id="current-year"></span>
Algolagnia answered 1/5, 2017 at 4:0 Comment(1)
Very easy and approach, love it!Townsman
I
2

with JavaScript:

Copyright &copy <script type="text/javascript"> document.write(new Date().getFullYear());</script> [companyName] Communications Inc. All rights reserved.

if you compile the above code somewhere like here , you would get an output as 2021 But for example, if you have a footer.vm file:

<div id="footerLinks" style="clear:left; text-align:left; color:#282828">
    Copyright &copy <script type="text/javascript"> document.write(new Date().getFullYear());</script> DigiVie Communications Inc. All rights reserved.
  </div>

and this one with the PHP:

&copy; <?php
  $fromYear = 2008;
  $thisYear = (int)date('Y');
  echo $fromYear . (($fromYear != $thisYear) ? '-' . $thisYear : '');?> CompanyName.
Interesting answered 16/2, 2021 at 18:17 Comment(0)
A
1

The very simple solution above that was posted by user3731460 and edited by user663031 worked perfectly.

Higher in the thread on this page, one of the commenters asked "What's wrong with using document.write (other than depending on the client to be set to the correct year)?"

I dont know the reasoning back in 2017 (when the question was originally asked here), but now (in oct 2020), lighthouse extension for chrome gives the warning 'avoid document.write because it can delay page load by tens of seconds.' the way google encourages user experience, i can see that google may penalize the use of document.write at some point.

Achernar answered 5/10, 2020 at 14:22 Comment(1)
The point of the comment was to get the OP to think about why document.write might be good or bad in this context. Not using it because "Google dev tools say not to use it" is not a logical argument.Murat
M
0

If you want to have the year in multiple places, use a class instead:

const year = new Date().getFullYear();
document.querySelectorAll(".js-year").forEach(el => el.innerText = year);
<span class="js-year"></span>
Mirellamirelle answered 27/8, 2022 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.