I am trying to read a CSV file. The date in the file in a format - 'DD-MMM-YY'. When I am trying to read this, I am getting an invalid date error. 01-Apr-20 - how do we read this in react?
Thanks In advance!
I am trying to read a CSV file. The date in the file in a format - 'DD-MMM-YY'. When I am trying to read this, I am getting an invalid date error. 01-Apr-20 - how do we read this in react?
Thanks In advance!
If you can afford to use an external library, you can use moment.js.
Then, to read these dates, simply use the following line:
moment('01-Apr-20', 'DD-MMM-YY')
which returns a moment object (basically like a Date-object), with a lot of functionality.
To install:
Node.js / npm: npm install moment
, then import it using require()
or import moment from 'moment'
.
Vanilla javascript: Add the following script tag to your html-code: <script src="https://momentjs.com/downloads/moment.js"></script>
.
You don't need to put that extra 2nd parameter. Only the 1st parameter date would suffice to convert it to a javascript date type object.
new Date('01-Apr-20')
If you need to show it in any particular format, you can use the date libraries packages e.g luxon (advantage of being less in size)
https://moment.github.io/luxon/docs/manual/formatting.html#presets