reactjs format date code example

Example 1: date format in react js

import React  from 'react';
import Moment from 'react-moment';

export default class MyComponent extends React.Component {
    render() {
        return (
            const dateToFormat = '1976-04-19';
            <Moment>{dateToFormat}</Moment>
        );
    }
}

Example 2: moment for react

npm install --save moment react-moment

Example 3: react date format

npm install moment --save

Example 4: how to trim dates in react

<span className="date timeago" title={ this.props.message.createdAt }>
            {new Intl.DateTimeFormat('en-GB', { 
                month: 'long', 
                day: '2-digit',
                year: 'numeric', 
            }).format(new Date(this.props.message.createdAt))}
      </span>

Tags:

Misc Example