3

I'm making a calendar in Portuguese on ReactJS.

Despite being something simple, I'm not able to change my library date "moment.js" to be in the portuguese language (brazil).

Already tried code:

<strong>{moment(currentDate).locale('pt-br').format('LLLL')}</strong>

But I don't work, it keeps bringing the date in English.

1: Date in English

This is the date format code:

const [currentDate, setCurrentDate] = useState(new Date())

<Col>
 <strong>{moment(currentDate).format('LLLL')}</strong>
</Col>

How do I make the date stay in Portuguese(Brazil)?

squillman
  • 13,363
  • 3
  • 41
  • 60

3 Answers3

5

I just decided to import the location:

import 'moment/locale/pt-br'

and add code:

moment.locale('pt-br')
1

You need import your location like that:

import 'moment/locale/pt'

`// import moment from 'moment'
// import 'moment/locale/pt'`



const App = () => {
  return (
    <h1>{moment(new Date()).locale('pt').format('LLLL')}</h1>
  )
}


ReactDOM.render(
  <React.StrictMode>
      <App />
  </React.StrictMode>,
  document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/locale/pt-br.min.js" integrity="sha512-1IpxmBdyZx3okPiZ14mzw6+pOGa690uDmcdjqvT310Kwv3NRcjvL/aOtoSprEyvkDdAb7ZtM2um6KrLqLOY97w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>


<div id="root"></div>
0

I have tried to use 'pt-br' like this:

import 'moment/locale/pt-br'
import moment from 'moment'

and it was not working in a React project with Typescript.

I got to fix it this way:

import 'moment/dist/locale/pt-br'

Just add the folder 'dist' while you're importing the language. I hope everything works accordingly.