React Get Current Month Start Date and End Date
In this tutorial, we will learn how to get the current month starting date and last date using React JS. To get the current month starting date and last date, we will use the built-in Date object. By the end of this tutorial, you will be able to display the current month starting date and last date in your React app.

written

reviewed

updated

Thanks for your feedback!
Your contributions will help us to improve service.
How to Get Current Month Start Date and End Date in React Js ?
In this example, we will discuss how to get the start date and the last date of the current month using React JS. We will use the built-in Date object and some helper methods to achieve this functionality.
Copied to Clipboard
26
<script type="text/babel">
const { useState } = React;
const App = () => {
const [firstDay, setFirstDay] = useState(null);
const [lastDay, setLastDay] = useState(null);
const handleButtonClick = () => {
const date = new Date();
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
setFirstDay(firstDayOfMonth);
setLastDay(lastDayOfMonth);
};
return (
<div className='container'>
<h3>React Get Current Month start Date and End Date</h3>
<button onClick={handleButtonClick}>Get Current Month Dates</button>
<p>Start Date: {firstDay && firstDay.toLocaleDateString('en-GB')}</p>
<p>End Date: {lastDay && lastDay.toLocaleDateString('en-GB')}</p>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Ad