. Advertisement .
..3..
. Advertisement .
..4..
In a web app’s development phase or that of other similar technology products, it would be frustrating and time-consuming to set the copyright year manually every time.
This tutorial focuses on using Javascript to get the current year for copyright. This approach allows for easy, automatic, and immediate updates in the date for users.
How To Get The Current Year For Copyright In Javascript
Method 1: Use The getFullYear() Method
The new Date object() often works depending on the browser’s time zone to display the date. The return format is always a string. Using the getFullYear() function helps simplify this year’s reinitialization process in any changes or modifications.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Test</title>
</head>
<body>
<script>document.write(new Date().getFullYear())</script>
</body>
</html>
In the example above, the code returns a number representing the given date’s current year. The example fails to pass a parameter to the construction so that the date object can store a current date.
When employing this approach, don’t forget to place a script tag before getting the DOM elements declared. All those practices should be accomplished before the elements are present on the page. This way, you can avoid unwanted errors.
With an HTML, the innerHTML property will set the element’s inner HTML with id parameter set to copyright. In the paragraph variable, backticks are used to build a template string and store a p element with a copyright message.
You can also edit the code in the template string to change the element’s structure.
Method 2: Use The exec() And Regex Method
The Date() always returns the value in the form of a string and initial start with a current year. In this approach, you can use a regex to execute the year’s first literals. Here, a regex comes in handy to grab for characters without calculating the year after 9999.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>test</title>
</head>
<body>
<script>document.write(/\d{4}/.exec(Date())[0])</script>
</body>
</html>
Method 3: Use The Document.write()
This approach works wonders in updating your copyright year automatically in the footer. First, you should create an HTML element, including a paragraph or a span. Then, don’t forget to add a CSS id.
Make sure your chosen ID is unique on the page. Under this element, add the Javascript code inside the <script> tags. There is no need to build an external file to store it.
Besides the innerText, textContent is also a great alternative. Yet, innerHTML will not be favorable as you don’t need to add any inner element tags. All you need is the year in plain text.
Conclusion
There are three main ways to get the current year for copyright in Javascript. Each method is suitable for different types of data and purposes. So check it again to get a suitable one.
Leave a comment