首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >两个从属日期选择器,其中开始日期永远不能大于结束日期

两个从属日期选择器,其中开始日期永远不能大于结束日期
EN

Stack Overflow用户
提问于 2020-03-28 18:13:23
回答 1查看 305关注 0票数 0

我正在使用日期反应选择器,并且有一个开始和结束日期/时间。在启动器选取器中,永远不可能选择较小的日期/时间。因此,显而易见的是在结束日期选择器中设置MinDate和MinTime。但是,DatePicker要求您同时设置MinTime和MaxTime。我不想在MaxTime中设置任何东西。有什么建议可以解决这个问题吗?

代码语言:javascript
运行
复制
import React, { useState } from "react";
import { useForm } from "react-hook-form";
import DatePicker, { setHours } from "react-datepicker";
import Moment from "react-moment";
import {
  Row,
  Col,
  Card,
  CardHeader,
  CardBody,
  Form,
  FormGroup,
  Label,
  Input,
  Button
} from "reactstrap";

const Insights = props => {
  const [startDate, setStartDate] = useState();
  const [endDate, setEndDate] = useState();
  const [endMinDate, setEndMinDate] = useState(new Date());
  const [endMinTime, setEndMinTime] = useState(new Date());

  const GetSearchForm = () => {
    const { register, handleSubmit, watch } = useForm();

    const timePickerStyle = { width: 96, important: "true" };

    const onSearch = data => {
      console.log(data);
      console.log(startDate);
    };

    const onDateStartChange = date => {
      setStartDate(date);
      setEndMinDate(date);
      setEndMinTime(date);
      alert(date);
    };

    const onDateEndChange = date => {
      setEndDate(date);
    };

    return (
      <Form onSubmit={handleSubmit(onSearch)}>
        <Row>
          <Col>
            <FormGroup>
              <Label for="exampleEmail">Account Id</Label>
              <Input
                type="number"
                name="account"
                id="account"
                placeholder="AccountId"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Email</Label>
              <Input
                type="email"
                name="email"
                id="email"
                placeholder="Email"
                innerRef={register}
              />
            </FormGroup>
          </Col>

          <Col>
            <FormGroup>
              <Label for="exampleEmail">xPage Id</Label>
              <Input
                type="number"
                name="xpageid"
                id="xpage"
                placeholder="xPage Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Content Devider Id</Label>
              <Input
                type="number"
                name="contentdevider"
                id="contentdeviderid"
                placeholder="Content Devider Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Custom Page Id</Label>
              <Input
                type="number"
                name="custompage"
                id="custompageid"
                placeholder="Custom Page Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Service</Label>
              <Input
                type="text"
                name="servicename"
                id="servicename"
                placeholder="Custom Page Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
        </Row>

        <Row>
          <Col xs="4">
            <FormGroup>
              <Label for="exampleEmail">Start</Label>
              <DatePicker
                isClearable
                innerRef={register}
                name="datetimestart"
                className={"form-control"}
                selected={startDate}
                onChange={date => onDateStartChange(date)}
                showTimeSelect
                timeFormat="HH:mm"
                timeIntervals={15}
                timeCaption="time"
                dateFormat="dd-MM-yyyy H:mm"
              />
            </FormGroup>
          </Col>
          <Col xs="4">
            <FormGroup>
              <Label for="exampleEmail">End</Label>
              <DatePicker
                isClearable
                innerRef={register}
                name="datetimeend"
                className={"form-control"}
                selected={endDate}
                onChange={date => onDateEndChange(date)}
                showTimeSelect
                timeFormat="HH:mm"
                timeIntervals={15}
                timeCaption="time"
                dateFormat="dd-MM-yyyy H:mm"
                maxDate={startDate}
                minTime={endMinDate}
                maxTime={startDate}
              />
            </FormGroup>
          </Col>
        </Row>

        <Button>Submit</Button>
      </Form>
    );
  };

  return (
    <Row>
      <Col xs="12" lg="12">
        <Card>
          <CardHeader>
            <i className="fa fa-align-justify"></i> Insights
          </CardHeader>
          <CardBody>
            <GetSearchForm></GetSearchForm>
            <div>
              startDate:
              {startDate == null ? "" : startDate.toString()}
            </div>
            <div>endDate:{endDate == null ? "" : endDate.toString()}</div>
          </CardBody>
        </Card>
      </Col>
    </Row>
  );
};

export default Insights;
EN

回答 1

Stack Overflow用户

发布于 2020-03-29 00:27:41

您可以在onDateEndChange函数中检查日期时间。为此,您需要:

  • 从结束日期选择器中删除最小和最大时间属性
  • 添加到开始日期和结束日期的onDateEndChange比较中;如果结束日期将早于或相似-不更新状态或更新状态的最小日期(例如,开始日期加一分钟)。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60899790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档