ttsssss
ttsssss

Reputation: 235

How do I change date from YYYY/MM/DD to MM/DD/YYYY in React Native app?

Currently the date is displayed w/ YYYY first but I would like to put the YYYY at the end.

It looks like this "2022-07-11 @ 3:00PM" but I want it to look like "July, 11th 2022 @ 3:00pm" or at least "07-11-2022 @ 3:00PM"

Here is the current code:

      <Text style={styles.centerText}>{gameDataObject.gameDate} @ {gameDataObject.gameStartTime && convertTime(gameDataObject.gameStartTime)}</Text> 
      </View>```

Upvotes: 0

Views: 65

Answers (1)

Prerna Budhraja
Prerna Budhraja

Reputation: 206

Make use of moment library: https://momentjs.com/docs/

Install it: yarn add moment

Then use it like this:

const initialDate = gameDataObject.gameDate
const formattedDate = moment(initialDate, 'YYYY-MM-DD').format('MMMM, Do YYYY')

FormattedDate will look like what you expect. You can refer further here: https://momentjs.com/docs/#/displaying/

Upvotes: 1

Related Questions