Shortest way to print current year in a website

Shortest way to print current year in a website

I need to update a few hundred static HTML pages that have the copyright date hard coded in the footer. I want to replace it with some JavaScript that will automatically update each year.
Currently I’m using:

Is this as short as it gets?

Solutions/Answers:

Solution 1:

Here’s the shortest I can get it:

<script>document.write(new Date().getFullYear())</script>

That will work in all browsers I’ve run across.

How I got there:

  • You can just call getFullYear directly on the newly-created Date, no need for a variable. new Date().getFullYear() may look a bit odd, but it’s reliable: the new Date() part is done first, then the .getFullYear().
  • You can drop the type, because JavaScript is the default; this is even documented as part of the HTML5 specification, which is likely in this case to be writing up what browsers already do.
  • You can drop the semicolon at the end for one extra saved character, because JavaScript has “automatic semicolon insertion,” a feature I normally despise and rail against, but in this specific use case it should be safe enough.
Related:  SVG support for Internet Explorer 8 and below

It’s important to note that this only works on browsers where JavaScript is enabled. Ideally, this would be better handled as an offline batch job (sed script on *nix, etc.) once a year, but if you want the JavaScript solution, I think that’s as short as it gets. (Now I’ve gone and tempted fate.)

Solution 2:

TJ’s answer is excellent but I ran into one scenario where my HTML was already rendered and the document.write script would overwrite all of the page contents with just the date year.

For this scenario, you can append a text node to the existing element using the following code:

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

Solution 3:

<script type="text/javascript">document.write(new Date().getFullYear());</script>

Solution 4:

If you want to include a time frame in the future, with the current year (e.g. 2017) as the start year so that next year it’ll appear like this: “© 2017-2018, Company.”, then use the following code. It’ll automatically update each year:

&copy; Copyright 2017<script>new Date().getFullYear()>2017&&document.write("-"+new Date().getFullYear());</script>, Company.

© Copyright 2017-2018, Company.

But if the first year has already passed, the shortest code can be written like this:

&copy; Copyright 2010-<script>document.write(new Date().getFullYear())</script>, Company.

Solution 5:

The JS solution works great but I would advise on a server side solution. Some of the sites I checked had this issue of the entire page going blank and only the year being seen once in a while.

Related:  AWS Lambda Error: “Cannot find module '/var/task/index'”

The reason for this was the document.write actually wrote over the entire document.

I asked my friend to implement a server side solution and it worked for him.
The simple code in php

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

Enjoy!

Solution 6:

Here’s the ultimate shortest most responsible version that even works both AD and BC.

[drum rolls…]

<script>document.write(Date().split` `[3])</script>

That’s it. 6 Bytes shorter than the accepted answer.

If you need to be ES5 compatible, you can settle for:

<script>document.write(Date().split(' ')[3])</script>