Pedro Nunes
Pedro Nunes

Reputation: 11

How can I change the date format?

I'm using the library react-native-masked-text to get the user birthday already formatted, and I need this to be in "DD/MM/YYYY" format for the user so I can show in that way in other screens. BUT I also need to get the value in "yyyy-MM-dd" format to send it to a API. How can I get the two formats?

The code is below:

  <View>
    <Label>Your birth date</Label>

    <TextInputMask
        type={'datetime'}
        options={{ format: 'DD/MM/YYYY' }}
        value={birthDate}
        onChange={value => this.setState({birthDate: value })}
    />
 </View>

Upvotes: 0

Views: 317

Answers (1)

Maycon Mesquita
Maycon Mesquita

Reputation: 4590

You can set the new format on State:

<View>
  <Label>Your birth date</Label>

  <TextInputMask
      type={'datetime'}
      options={{ format: 'DD/MM/YYYY' }}
      value={birthDate}
      onChange={value => {
        const date = value.split('/');
        const birthDateAnotherFormat = date[2] + '/' + date[1] + '/' + date[0];

        this.setState({
          birthDate: value,
          birthDateAnotherFormat
        });
      }}
  />
</View>

Upvotes: 1

Related Questions