【问题标题】:How to get and modify the Date value如何获取和修改日期值
【发布时间】:2019-10-01 13:43:18
【问题描述】:

我正在处理一个 Reactjs 项目,我必须设置保修日期。 此保修日期为安装日期 + 1 年。

在我创建客户端时的代码中,我可以输入安装日期。此日期由日期选择器 react-datepicker 输入。

这是我的代码,解决方案:

class CreateClient extends React.Component {
  state = {
    installDate: null,
    warrantyDate: null
  };

  handleChange = date => {
    const warrantyDate = new Date(new Date(date).getTime() + 86400000 * 365);
    this.setState({
      installDate: date,
      warrantyDate
    });
  };

  render() {
    const { classes, translate, ...props } = this.props;
    return (
      <Create {...props} title={<ClientCreateTitle />}>
        <TabbedForm>
          <FormTab label="resources.client.tabs.identity">
            <TextInput autoFocus source="name" required />
            <TextInput
              type="email"
              source="email"
              validation={{ email: true }}
              formClassName={classes.email}
              validate={[required(), email()]}
            />
            <TextInput
              type="phone"
              source="phone"
              validation={{ phone: true }}
              formClassName={classes.phone}
            />
          </FormTab>
          <FormTab label="resources.client.tabs.address" path="address">
            <LongTextInput
              source="address"
              formClassName={classes.address}
              label="resources.client.localisation.address"
              required
            />
            <TextInput
              source="zipcode"
              formClassName={classes.zipcode}
              label="resources.client.localisation.zipcode"
              required
            />
            <TextInput
              source="city"
              formClassName={classes.city}
              label="resources.client.localisation.city"
              required
            />
            <TextInput
              source="country"
              formClassName={classes.country}
              label="resources.client.localisation.country"
              required
            />
          </FormTab>
          <FormTab label="resources.client.tabs.equipment" path="equipment">
            {/* <DateInput
          source="installAt"
          label="resources.client.info.installationDate"
        /> */}
            <DatePicker
              selected={this.state.installDate}
              onChange={this.handleChange}
            />
            <DisabledInput
              source="warrantyDate"
              label="Date de garantie (Automatique)"
              defaultValue={this.state.warrantyDate}
            />
          </FormTab>
        </TabbedForm>
      </Create>
    );
  }
}

export default withStyles(styles)(CreateClient);

我不知道如何设置保修日期。

为 Dupocas 编辑

这种方法有 2 个错误:

Warning: Failed prop type: Invalid prop `value` supplied to `TextField`.
Warning: Failed prop type: Invalid prop `value` supplied to `Input`.

【问题讨论】:

    标签: reactjs datepicker


    【解决方案1】:

    假设以下日期

    const date = new Date('2019-10-01')
    

    增加日期的许多方法之一是使用getTime() 并添加对应的ms,然后再将其转换回清晰的date

       const date = new Date('2019-10-01')
       const oneYearFromNowInMs = date.getTime() + (86400000 * 365) //ms in a day times 365
       const newDate = new Date(oneYearFromNowInMs)
       console.log(newDate)

    现在假设Component

    class Component extends React.Component {
        state = {
            startDate : null,
            warrantyDate : null
        }
    
        handleChange = date => {
            const warrantyDate = new Date(new Date(date).getTime() + (86400000*365))
            this.setState({
                startDate: date,
                warrantyDate
            })
        }
    
        render(){
            return(
                <DatePicker
                    selected={this.state.startDate}
                    onChange={this.handleChange}
                 />
            )
        }
    }
    

    如果您需要执行许多涉及日期的操作,您一定要看看date-fns

    【讨论】:

    • 好的,我明白了。我写了这个const warrantyDate = installDate.getTime() + (86400000 * 365),但是我可以在一个类中写一个 const 吗?我不知道语法
    • 我正在更新我的答案以包含一个反应的用例
    • 现在看起来更好
    • defaultValue={this.state.warrantyDate} 保修日期现在是状态的一部分
    • 这个方法有2个错误,请检查编辑后的帖子。
    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2015-10-11
    • 2017-12-16
    • 2010-09-19
    相关资源
    最近更新 更多