close

[Solved] React JS get current date

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error React JS get current date in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How React JS get current date Error Occurs?

Today I get the following error React JS get current date in javascript.

How To Solve React JS get current date Error ?

  1. How To Solve React JS get current date Error ?

    To Solve React JS get current date Error You can use the react-moment package
    -> https://www.npmjs.com/package/react-moment
    Put in your file the next line:
    import moment from "moment"; date_create: moment().format("DD-MM-YYYY hh:mm:ss")

  2. React JS get current date

    To Solve React JS get current date Error You can use the react-moment package
    -> https://www.npmjs.com/package/react-moment
    Put in your file the next line:
    import moment from "moment"; date_create: moment().format("DD-MM-YYYY hh:mm:ss")

Solution 1

Your problem is that you are naming your component class Date. When you call new Date() within your class, it won’t create an instance of the Date you expect it to create (which is likely this Date)- it will try to create an instance of your component class. Then the constructor will try to create another instance, and another instance, and another instance… Until you run out of stack space and get the error you’re seeing.

If you want to use Date within your class, try naming your class something different such as Calendar or DateComponent.

The reason for this is how JavaScript deals with name scope: Whenever you create a newly named entity if there is already an entity with that name in scope, that name will stop referring to the previous entity and start referring to your new entity. So if you use the name Date within a class named Date, the name Date will refer to that class and not to any object named Date which existed before the class definition started.

Solution 2

You can use the react-moment package

-> https://www.npmjs.com/package/react-moment

Put in your file the next line:

import moment from "moment";

date_create: moment().format("DD-MM-YYYY hh:mm:ss")

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment