简体   繁体   中英

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM