3

如何获取开始日期和结束日期的值?我用:

import { DateRangePickerComponent } from '@syncfusion/ej2-react-calendars';

这是一个重复的问题,因为我想分别获取开始日期和结束日期。

import { DateRangePickerComponent } from '@syncfusion/ej2-react-calendars';
import { makeStyles } from '@material-ui/core/styles';

const [startdate, setStartDate] = useState("");
const [enddate, setEndDate] = useState("");
const onChangedate = (props) => {
  const stateDate = props.startDate;
  const endDate = props.endDate;
  setStartDate(stateDate)
  setEndDate(endDate)
};

const handleUpdateButton = async ()=> {
  console.log(startdate, enddate) // I just want to print here the start date and end date
}

<Grid item xs={12} sm={6} md={6} lg={6} style={{padding: 15, paddingBottom: 0, alignItems: 'center'}}>
  <DateRangePickerComponent id="daterangepicker" change={onChangedate} placeholder='Select a range' style={{paddingTop: 10, fontSize: 16}}/>
</Grid>

<Grid item xs={12} style={{padding: 8}}>
 <Button onClick={handleUpdateButton} variant="contained"color="primary" fullWidth>
   Apply Filters
 </Button>
</Grid>

在此处输入图像描述

4

1 回答 1

2

您可以使用startDateendDate事件 分别change访问日期DateRangePickerComponent

import { useState } from "react";
import { DateRangePickerComponent } from "@syncfusion/ej2-react-calendars";

const App = () => {
  const [startDate, setStartDate] = useState();
  const [endDate, setEndDate] = useState();

  function convertDate(inputFormat) {
    function pad(s) { return (s < 10) ? '0' + s : s; }
    var d = new Date(inputFormat)
    return [pad(d.getMonth()+1),pad(d.getDate()), d.getFullYear()].join('-')
  }

  const onChange = (e) => {
    let startDate = convertDate(e.startDate);
    let endDate = convertDate(e.endDate);
    setStartDate(startDate);
    setEndDate(endDate);
  };

  return (
    <div className="container">
      <DateRangePickerComponent change={onChange} />
      <div>
        {startDate} - {endDate}
      </div>
    </div>
  );
};

export default App; 

编辑同步融合日历(分叉)

于 2021-07-29T20:18:00.927 回答