In JavaScript, you can find the current year using the Date
object and the getFullYear()
method.
Here's an example code block:
index.tsx137 chars4 lines
We first create a new Date
object using the new
keyword. This will initialize the object with the current date and time by default.
We then call the getFullYear()
method on the Date
object to retrieve the current year. This method returns a number representing the year (such as 2021
).
Finally, we log the value of currentYear
to the console using console.log()
. This will print the output to the browser console (or the terminal console if you're using JavaScript outside of a web browser).
gistlibby LogSnag