简体   繁体   English

如何更改日期格式?

[英]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.我正在使用库react-native-masked-text来获取已经格式化的用户生日,并且我需要将其设置为用户的“DD/MM/YYYY”格式,以便我可以在其他屏幕中以这种方式显示。 BUT I also need to get the value in "yyyy-MM-dd" format to send it to a API.但我还需要以“yyyy-MM-dd”格式获取值,以将其发送到 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:您可以在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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