Development

Current Year Copyright in JavaScript

Captain Salem 2 min read

Current Year Copyright in JavaScript

I am willing to bet that if you visited nearly 90% of all websites on planet earth, you are bound to find a form of copyright inidcator. This can be in the footer of the website, in contact us page, or indiviual pages.

Copyright statements are very useful for protecting intellectual property of informing about the ownership of certain digital content.

A major component of copyright signation is the current year which indicates when the work was created or updated. However, instead of manually changing the year value when a new year rolls, we as developers choose to take advantage of dynamic year update using Javascript.

In this tutorial, we are going to learn about the several methods that we can use to fetch the current year in the context of copyright designation.

Method 1 - Using JavaScript’s Date Object

The most basics technique is using the Date object for working with dates and time values. We can use this objeect to fetch the current year in JavaScript as shown:

const cur = new Date().getFullYear();
console.log(`Copyright © GeekBits LLC ${cur}`);

In the example code above, we create a new Date object and use it to fetch the current year using the getFullYear() method. We then incorporate this into a copyright string.

Output:

Copyright © GeekBits LLC 2024

Method 2 - Using the new Date() Constructor

Another way to get the current year is by using the new Date() constructor and extracting the year portion. This is basically the same method as the previous one but it seperates the creation of Date object and date extraction.

const date = new Date();
const cur = date.getFullYear();
console.log(`Copyright © GeekBits LLC ${cur}`);

This should return a similar output.

Method 3 - Using the Internationalization API

JavaScript’s Internationalization API provides a way to format dates in a locale-specific manner. We can use it to get the current year.

const cur = new Intl.DateTimeFormat('en-US', { year: 'numeric' }).format(new Date());
console.log(`Copyright © GeekBits LLC ${cur}`);

In the example above, we use the Int.DateTimeFormat() method and specify the target locale to en-US and request the year in numeric format.

The result:

Copyright © GeekBits LLC 2024

Method 4 - Using External Libraries

Like any other lazy JavaScript developer, we can use external library to achieve these. For example, we can use the moment.js or Luxon libraries which are powerful for working with date and time values.

The following shows how to get current year usign moment.js:

const cur = moment().format('YYYY');
console.log(`Copyright © GeekBits LLLC ${cur}`);

To use Luxon, we can use the query:

const cur = luxon.DateTime.local().toFormat('yyyy');
console.log(`Copyright © GeekBits LLC ${cur}`);

Boom.

Conclusion

In this tutorial, we learned how we can use the various JavaScript method to dynamically adjust the year value in copyright statements.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.