0 votes
in ReactJS by

How to format date using React Intl?

1 Answer

0 votes
by

The injectIntl() higher-order component will give you access to the formatDate() method via the props in your component. The method is used internally by instances of FormattedDate and it returns the string representation of the formatted date.

import { injectIntl, intlShape } from 'react-intl'

const stringDate = this.props.intl.formatDate(date, {
  year: 'numeric',
  month: 'numeric',
  day: 'numeric'
})

const MyComponent = ({intl}) => (
  <div>{`The formatted date is ${stringDate}`}</div>
)

MyComponent.propTypes = {
  intl: intlShape.isRequired
}

export default injectIntl(MyComponent)

Related questions

0 votes
asked Mar 3, 2020 in ReactJS by miceperry
+1 vote
asked Mar 2, 2020 in ReactJS by RShastri
...