How to Get the Current Year in JavaScript
Table of Contents
In this post, we'll learn how to get the current year in JavaScript.
The best way to get the current year is to use the getFullYear()
method.
This method is defined on the Date
object which comes native with JavaScript.
This means that you can use this method both in the browser and in server-side JavaScript like Node and Deno.
To get the current year, you can use the following code:
JAVASCRIPTconst date = new Date();
const currentYear = date.getFullYear();
console.log(currentYear);
BASH2022
The getFullYear()
method returns the current year in four digits. The method supports year values from 1000 to 9999.
Use the editor below to test out getFullYear()
in the browser.
- JavaScript
Hit the Run
button to see the result.
Conclusion
In this post, we've seen how to get the full year in JavaScript.
Remember, this is possible on both the browser and server-side JavaScript like Node and Deno.
Hopefully, you found this content useful. Happy coding!
Resources
- Getting Started with TypeScript
- Managing PHP Dependencies with Composer
- Create an RSS Reader in Node
- Getting Started with Electron
- Git Tutorial: Learn how to use Version Control
- How to Serve Static Files with Nginx and Docker
- How to deploy a .NET app using Docker
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting User Location using JavaScript's Geolocation API
- How To Create a Modal Popup Box with CSS and JavaScript