One way is to place the current year, obtained using new Date(). getFullYear() , in a " <span> " or " <div> " element, and then inserting that SPAN or DIV into the web page.
Use the getFullYear() method to get the current year for copyright, e.g. new Date(). getFullYear() . The getFullYear method returns the current year when called on the current date.
To get the current year, use the getFullYear() JavaScript method. JavaScript date getFullYear() method returns the year of the specified date according to local time. The value returned by getFullYear() is an absolute number.
Years later, when doing something else I was reminded that Date()
(without new
) returns a string, and a way that's one character shorter that my original below came to me:
<script>document.write(/\d{4}/.exec(Date())[0])</script>
The first sequence of four digits in the string from Date()
is specified to be the year. (That wasn't specified behavior — though it was common — when my original answer below was posted.)
Of course, this solution is only valid for another 7,979 years (as of this writing in 2021), since as of the year 10000 it'll show "1000" instead of "10000".
You've asked for a JavaScript solution, so 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:
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()
.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.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.)
However, unless you're using a server that can only provide static files, you're probably better off doing this on the server with a templating engine and using caching headers to allow the resulting page to be cached until the date needs to change. That way, you don't require JavaScript on the client. Using a non-defer
/async
script
tag in the content also briefly delays the parsing and presentation of the page (for exactly this reason: because the code in the script might use document.write
to output HTML).
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>
©
<span id="copyright">
<script>document.getElementById('copyright').appendChild(document.createTextNode(new Date().getFullYear()))</script>
</span>
Company Name
</div>
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:
© 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:
© Copyright 2010-<script>document.write(new Date().getFullYear())</script>, Company.
<script type="text/javascript">document.write(new Date().getFullYear());</script>
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With