【问题标题】:How can I change the date format?如何更改日期格式?
【发布时间】:2020-05-28 19:09:29
【问题描述】:

我正在使用库react-native-masked-text 来获取已经格式化的用户生日,并且我需要为用户将其设为“DD/MM/YYYY”格式,以便我可以在其他屏幕上以这种方式显示。但我还需要以“yyyy-MM-dd”格式获取值以将其发送到 API。如何获得这两种格式?

代码如下:

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

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

【问题讨论】:

    标签: javascript reactjs react-native date-formatting


    【解决方案1】:

    您可以在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-28
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      相关资源
      最近更新 更多