I was asked by a visitor how he could insert the current year into his web page and have it automatically update every year without manual intervention on his part. This can be easily done by inserting a bit of JavaScript into the portion where you normally place the year.
If you are doing this because you want to change the copyright date to include the current year, you are giving yourself unnecessary work. Please read my article on Copyright Issues for Webmasters for more details. Essentially, the creator of an original piece of work automatically holds the copyright for that article when he/she creates it, and it lasts for the rest of his/her life, plus an additional 50 years (or more, in some countries) after his/her death.
In other words, the date you place on your copyright notice is irrelevant for the purpose of the copyright term.
The real secret to extending your copyright is to live longer.
Since the method described in this article uses JavaScript, you will need to know, at the very least, how to insert HTML into your web page. Otherwise, you will be left staring at the code I provide below, wondering how to use it. In brief, if you use a visual web editor like Expression Web or BlueGriffon, you will need to switch the editor into its HTML mode and insert the code there.
You can find tutorials for inserting HTML code for Expression Web, BlueGriffon, Dreamweaver, KompoZer and Nvu by clicking the relevant link in this sentence.
If you wish to display the current year in your visitor's local time zone, insert one of the following blocks of code at the location you will normally type the year.
Use the following for pages written in HTML 4.01.
Use the block of code below if your web page is in HTML5.
If you use a visual web editor like
Expression Web 4
or Dreamweaver CS6,
your pages will, by default, be in XHTML 1.0, provided you did not change the settings. However, unless you went out of your way
to configure your web server (note that I said web server, not browser or web editor) to deliver your
web pages using the "application/xhtml+xml
" MIME type, instead of the default "text/html
",
you should be able to use the HTML 4.01 code as given above. In other words, skip this section and scroll back to
the earlier HTML 4.01 section.
If you don't understand what I meant about configuring the web server to deliver your pages in a different MIME type (eg, you are wondering, "What is MIME? What is a web server? How do I configure it? Did I do this?"), chances are that you didn't. If you did, my words would probably have rung a bell. It's not something a typical webmaster does. You would have been forcing the browser to treat your web page as an XML document instead of a simple HTML web page.
For the uber-geeks who have actually changed the MIME type of your web pages , there is no quick and easy solution for you.
You cannot use "document.write()
" to insert the date the way I mentioned earlier. But you probably
already knew that, since getting a web page to be treated as an XML document has many implications, this
being one of them.
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.
Alternatively, if the above sounds like too much work (or you didn't understand it), revert your server
to its default behaviour of delivering all web pages as "text/html
" and just use the code in the
HTML 4.01 section. Yes, I know it's not what the XHTML purists say, but it's either that, or do things
the longer way. Besides, if your web page is only meant to be displayed by a web browser, and not processed
by some bespoke (ie, custom) program for a specialized situation, why force it to be treated
as an XML document? You're just giving yourself unnecessary headaches. Unless you are into masochism, it's possible
to reap the benefits of XHTML without having to endure the additional restrictions that a strict
XML-parsing regime imposes.
Anyway, these days, the future of the web is HTML5, so all these old and tired arguments for and against are probably
academic.
If you are not sure which whether your page uses HTML 4.01 or HTML5, just use the HTML 4.01 code (which is valid in both HTML 4.01 and HTML5). Although I put the ideal code for each version in accordance with the recommendations of the web standards of that version, in practice, all modern web browsers treat both snippets identically.
Demo: the current year (in your local time) is .
The HTML code for the above paragraph, to serve as an example for those who are still unsure where to embed the code on your page, is:
Those who want to print the year in Coordinated Universal Time (that is, UTC or GMT) should use one of the following instead.
If your page is in HTML 4.01, use the code below.
Web pages in HTML5 can use the following.
See the earlier discussion on XHTML.
The same thing applies, except that you should use getUTCFullYear()
instead of getFullYear()
to get the current year in UTC.
Demo: the current year in UTC is .
For most of the year, the numbers displayed by both the local time and UTC snippets will be identical. Depending on your location, it may be different during certain hours on the 31st of December and the 1st of January, when the year changes in one part of the world, but not yet in another.
Copyright © 2018-2019 Christopher Heng. All rights reserved.
Get more free tips and articles like this,
on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as:
How to Make a Web Page Always Display the Current Year