Formatting Dates in Create React App with MomentJS

While working with React, I found I wanted a way to work with dates similiar to the way Angular uses the DatePipe to handle displaying dates correctly. The answer is in using the momentjs library.

To do so, add the package to your project:

npm i --save moment

Then add the following code to test in one of your .js or .tsx files:

import moment from 'moment';

...

{moment().format()}

You should see an ISO string being displayed. To check on ways you can use the library, check the docs on the page above.

Leave a Reply

Your email address will not be published. Required fields are marked *