Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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 */
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading