X

LATEST #JQUERY NEWS

jQuery Snippets: Getting the Current Year


Here’s a fun, simple, and incredibly useful jQuery that you can probably use on just about every single one of your projects. This snippet uses jQuery to grab the current year and display it wherever you’d like within your webpage. This is a great chunk of code to have on hand.

One particularly good use for this snippet is in the copyright section of your web page or project. Most websites have a copyright notice, followed by the current year, somewhere on their web pages (usually you’ll find it down towards the footer or in the footer, but not always).

We’ve all probably seen sites with copyrights that are super outdated. When you see a copyright date from a few years prior, chances are that the website owners hard-coded the date into their HTML, which is alright, if you are capable of or can remember to change the copyright date every single year. Most people, it’s probably safe to assume, would rather not have to worry about it. That’s where jQuery comes in.

With the following jQuery snippet, the current year is grabbed and loaded each and every time the page loads, so your users will never be looking at an outdated copyright that is boasting the wrong year. Take a look at the snippet below so you can use it for yourself on your future and current projects:

$(function () {
 thisyear = new Date().getFullYear();
 $('.currentyear').text(thisyear);
});

The snippet above is super straightforward. It uses the .getFullYear() method to grab the current full year (so for example, 2017 instead of ’17) and then it uses the .text() method to add the year to the HTML element that has the .currentyear class tag by passing the “thisyear” variable through the .text() method. Easy as pie. For an example of the HTML that might accompany this jQuery to display your copyright year, take a look at the snippet below:

<p>© <span class=".currentyear"></span></p>

When used with the jQuery snippet in this tutorial, the current year will be dynamically added within those span tags, and you’ll never have to spare another thought for your copyright line again.

Recently Published

article image
»

Changing images in a loop on hover using jQuery

Why use this Function? Changing images in a loop on mouse over is a ...

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...