Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get "Maximum call stack size exceeded" error when trying to convert current month number to current month name [closed]

In the project, I want to write down the current day and month like this:

Today is the 13th of September

When getting the current day, there is no error. But, when converting the current month number to the current month name, I get the error below:

Date.jsx:4 Uncaught RangeError: Maximum call stack size exceeded

My code in the Date component:

function Date() {
  let date = new Date()
  // console.log(dayDate.getDate())

  const takeMonth = (month) => {
    const monthName = date.setMonth(month - 1)
    return monthName.toLocaleString("default", { month: "long" })
  }
  return <div> Date {takeMonth(3)}</div>
}

All answers are appreciated!

like image 651
Recep Çankaya Avatar asked Nov 07 '25 21:11

Recep Çankaya


1 Answers

You are currently overriding the Date function by defining your own function called Date, so when you call new Date() inside your function, you are in fact calling your own override, which creates an infinite loop...

You should rename your own function like:

function getDate() {
  let date = new Date()
  /* rest of the code */
}
like image 189
Ben Avatar answered Nov 09 '25 11:11

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!