Javascript date this year

JavaScript Date getFullYear()

getFullYear() returns the full year (4 digits) of a date.

Syntax

Parameters

Return Value

Browser Support

getFullYear() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Javascript date this year

Last updated: Jan 4, 2023
Reading time · 3 min

banner

# Table of Contents

# Get the Current Year in JavaScript

To get the current year:

  1. Use the new Date() constructor to get a Date object.
  2. Call the getFullYear() method on the Date object.
  3. The getFullYear method will return the current year.
Copied!
// 👇️ Get current Year const currentYear = new Date().getFullYear(); console.log(currentYear); // 👉️ 2023

We used the Date() to get a Date object, on which we can call various methods.

Copied!
const date = new Date(); console.log(date); // 👉️ 2023-01-04T05:21:48.504Z console.log(date.getFullYear()); // 👉️ 2023

The last step is to use the Date.getFullYear method to get the current year.

Читайте также:  Java jlabel change text

The method returns the year of the specified date according to local time.

Notice that we created a new date object that represents the current date and time using the new Date() constructor.

If you call the method on a Date object that stores a different date and time, the output is reflected.

All you have to do is pass the specific date into the Date constructor on which you call the methods.

Copied!
const date = new Date('January 04, 2025 05:24:07'); const yearOfDate = date.getFullYear(); console.log(yearOfDate); // 👉️ 2025

If you have to do this often, define a reusable function.

Copied!
function getCurrentYear() return new Date().getFullYear(); > const currentYear = getCurrentYear(); console.log(currentYear); // 👉️ 2023

The getCurrentYear function takes no parameters and returns the current year.

If you only need the last 2 digits of the current year, use the slice() method.

Copied!
function getCurrentYear() return String(new Date().getUTCFullYear()).slice(-2); > const currentYear = getCurrentYear(); console.log(currentYear); // 👉️ 23

We used the String.slice() method to get the last 2 digits of the current year.

# Get the Last 2 Digits of the current Year

If you need to get the last 2 digits of the current year, use the slice() method.

Copied!
// 👇️ Get the last 2 digits of the current year const last2 = new Date().getFullYear().toString().slice(-2); console.log(last2); // 👉️ '23' const last2Num = Number(last2); // 👇️ Get last 2 digits of a Year const date = new Date('April 04, 2025 05:24:07'); const last2Again = date.getFullYear().toString().slice(-2); console.log(last2Again); // 👉️ '25'

The only argument we passed to the slice method is the start index — the index at which we start extraction of characters from the string.

We used a negative index of -2 to get the last 2 digits of the year.

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.

Copied!
const currentYear = new Date().getFullYear(); console.log(currentYear); // 👉️ 2022

Here is an example that injects the current year into an HTML element directly on the page.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> head> body> div>Content herediv> div id="copyright">div> script> const paragraph = `

Copyright © $new Date().getFullYear()> John Doe

`
; document.getElementById('copyright').innerHTML = paragraph;
script> body> html>

And here is an example that displays the current date for copyright in a React.js component using JSX.

Copied!
export const Footer = () => ( div> p> Copyright © new Date().getFullYear()> John Doe p> div> );

The Date() constructor creates a new Date object.

We didn’t pass any parameters to the constructor when creating the Date , so it created a Date object that stores the current date.

The Date.getFullYear method returns a number that represents the year of the given date.

If you add the code snippet that uses HTML, make sure to place the script tag at the bottom of the body tag, after all of the DOM elements have been declared.

Placing the script tag before declaring the DOM elements would run it before the elements exist on the page and cause an error.

In the HTML example, we used the innerHTML property to set the inner HTML of the element with id set to copyright .

The paragraph variable uses backticks (not single quotes) to create a template string that stores a p element that contains the copyright message.

Here is how the p element looks on the page.

current year for copyright

My blog uses the same approach to display the current year at the footer.

You could change the structure or the style of the element by editing the HTML code in the template string.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Оцените статью