In React, obtaining the current year may seem like a simple task, yet it plays a crucial role in various applications, especially when displaying dynamic content or creating time-sensitive features. We’ll explore two practical methods to achieve this: utilizing JavaScript’s Date object and leveraging React’s state management to update the year dynamically.

Using JavaScript’s Date Object
We’ll begin by defining a method, getCurrentYear, which utilizes the Date object to fetch the current year.
const getCurrentYear = () => {
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
return currentYear;
};
Obtaining the Current Month
Similarly, we’ll create a function, getCurrentMonth, to retrieve the current month. Note that JavaScript’s getMonth() method returns the month starting from 0, hence the addition of 1 to display the correct month number.
const getCurrentMonth = () => {
const currentDate = new Date();
const currentMonth = currentDate.getMonth() + 1;
return currentMonth;
};
Integrating Methods in a React Component
Now, let’s integrate these functions into a React component. This component will display the current year and month, updating them dynamically upon button clicks.
import React, { useState } from 'react';
const App = () => {
const getCurrentYear = () => {
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
return currentYear;
};
const getCurrentMonth = () => {
const currentDate = new Date();
const currentMonth = currentDate.getMonth() + 1;
return currentMonth;
};
const [year, setYear] = useState(0);
const [month, setMonth] = useState(0);
const updateYear = () => {
const updatedYear = getCurrentYear();
setYear(updatedYear);
};
const updateMonth = () => {
const updatedMonth = getCurrentMonth();
setMonth(updatedMonth);
};
return (
<div>
<p>Current Year: {year}</p>
<button onClick={updateYear}>Update Year</button>
<p>Current Month: {month}</p>
<button onClick={updateMonth}>Update Month</button>
</div>
);
};
export default App;
Result:
Here’s how the page looks like before we click the buttons:

After clicking the buttons and getting updated values(It’s December 2023 for me while writing):

Code Explanation:
Let’s break down the code and explanation into concise points to delve deeper into howthe App component operates:
State Initialization:
- const [year, setYear] = useState(0);
- Initializes the
yearstate usinguseStatehook with an initial value of0. setYearis the function used to update theyearstate.
- Initializes the
- const [month, setMonth] = useState(0);
- Initializes the
monthstate usinguseStatehook with an initial value of0. setMonthis the function used to update themonthstate.
- Initializes the
Getting the year using const getCurrentYear = () => { … }
- Defines a function
getCurrentYearthat uses theDateobject to retrieve the current year. getCurrentYear()returns the current year value.
Getting the month const getCurrentMonth = () => { … }
- Defines a function
getCurrentMonththat uses theDateobject to retrieve the current month. - The month retrieved is incremented by 1 to match standard month numbers (January as 1, February as 2, and so on).
Updating year using const updateYear = () => { … }
- Defines a function
updateYearto update theyearstate. - Calls
getCurrentYear()to fetch the current year and usessetYearto update the state.
Updating month const updateMonth = () => { … }
- Defines a function
updateMonthto update themonthstate. - Calls
getCurrentMonth()to fetch the current month and usessetMonthto update the state.
Return Statement:
- JSX Structure:
- Renders a <div> containing:
<p>displaying the current year:Current Year: {year}<button>triggeringupdateYearfunction on click:Update Year<p>displaying the current month:Current Month: {month}<button>triggeringupdateMonthfunction on click:Update Month
- Renders a <div> containing:
Event Handling:
- onClick Events:
- When the
Update Yearbutton is clicked, it triggers theupdateYearfunction, updating theyearstate with the current year. - When the
Update Monthbutton is clicked, it triggers theupdateMonthfunction, updating themonthstate with the current month.
- When the
Conclusion
In this blog, we explored practical methods using JavaScript’s Date object and React’s state management to dynamically retrieve and display the current year and month in a React application. Leveraging these methods provides a seamless way to showcase real-time date information and enables easy updates through user interactions.
Leave a Reply