gpt4 book ai didi

Reactjs如何获取开始日期和结束日期的值?

转载 作者:行者123 更新时间:2023-12-04 14:53:41 25 4
gpt4 key购买 nike

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

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>

enter image description here

最佳答案

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

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;

Edit syncfusion-calendar (forked)

关于Reactjs如何获取开始日期和结束日期的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68580689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com