Last updated: Apr 7, 2024
Reading time·2 min
Use the Date()
constructor to get the current year in React, e.g.
new Date().getFullYear()
.
The getFullYear()
method will return a number that corresponds to the
current year.
const App = () => { console.log(new Date().getFullYear()); return ( <div> <div>{new Date().getFullYear()}</div> <br /> <div> Copyright © {new Date().getFullYear()} Bobby Hadz </div> </div> ); }; export default App;
We used the
Date() constructor
to get a Date
object on which we can call various methods.
const now = new Date(); console.log(now.getFullYear()); // 👉️ 2023
The Date.getFullYear method returns a four-digit number that represents the year of the date.
Notice that we had to wrap the call to the getFullYear()
method in curly
braces in our JSX code.
<div>{new Date().getFullYear()}</div>
The JavaScript code between the curly braces will be evaluated with the current year.
Other commonly used methods on the Date
object are:
0
(January) and 11
(December) and represents
the month for a given date. Yes, unfortunately, the getMonth
method is off
by 1
.const App = () => { const year = new Date().getFullYear(); const month = new Date().getMonth() + 1; const day = new Date().getDate(); return ( <div style={{padding: '150px'}}> <div>{new Date().getFullYear()} - year</div> <div>{new Date().getMonth() + 1} - month</div> <div>{new Date().getDate()} - day of month</div> <br /> <div> Copyright © {new Date().getFullYear()} Bobby Hadz </div> </div> ); }; export default App;
These methods allow you to get the year/month/day for any date object, it doesn't have to be the current year.
const App = () => { const date = '2025-07-21'; const year = new Date(date).getFullYear(); const month = new Date(date).getMonth() + 1; const day = new Date(date).getDate(); return ( <div> <div>{new Date(date).getFullYear()} - year</div> <div>{new Date(date).getMonth() + 1} - month</div> <div>{new Date(date).getDate()} - day of month</div> <br /> <div> Copyright © {new Date(date).getFullYear()} Bobby Hadz </div> </div> ); }; export default App;