Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用mui datepicker筛选Reactjs中特定日期的日期_Reactjs_Date_Filter_Datepicker - Fatal编程技术网

如何使用mui datepicker筛选Reactjs中特定日期的日期

如何使用mui datepicker筛选Reactjs中特定日期的日期,reactjs,date,filter,datepicker,Reactjs,Date,Filter,Datepicker,我有一个对象数组“mainData”,如下所示: 现在,我允许用户使用mui日期选择器选择开始日期和结束日期。这就是我接收值的方式: db date new : Sat Jul 25 2020 16:44:43 selected fromDate : Sat Jul 25 2020 22:46:00 selected toDate : Mon Aug 10 2020 22:46:15 useEffect(() => { if (fromDate !== null &a

我有一个对象数组“mainData”,如下所示:

现在,我允许用户使用mui日期选择器选择开始日期和结束日期。这就是我接收值的方式:

db date new : Sat Jul 25 2020 16:44:43
selected fromDate : Sat Jul 25 2020 22:46:00
selected toDate : Mon Aug 10 2020 22:46:15

 useEffect(() => {
        if (fromDate !== null && toDate !== null) {
            setReportData(
                mainData.filter(
                    (obj) => {

                        console.log("db date new :", new Date(obj.date.substring(0, 19)))
                        console.log("selected fromDate :",fromDate)
                        console.log("selected toDate :", toDate)

                        
                        return new Date(obj.date.substring(0, 19)).getTime() >= fromDate.getTime() && new Date(obj.date.substring(0, 19)).getTime() <= toDate.getTime()

                     
                    }
                )
            )

          

        }

    }, [toDate])

第一个db日期是25号,其中as fromdate也是25号,但由于时间/时区的不同,它们的值不同

这是我用来过滤掉的值:

db date new : Sat Jul 25 2020 16:44:43
selected fromDate : Sat Jul 25 2020 22:46:00
selected toDate : Mon Aug 10 2020 22:46:15

 useEffect(() => {
        if (fromDate !== null && toDate !== null) {
            setReportData(
                mainData.filter(
                    (obj) => {

                        console.log("db date new :", new Date(obj.date.substring(0, 19)))
                        console.log("selected fromDate :",fromDate)
                        console.log("selected toDate :", toDate)

                        
                        return new Date(obj.date.substring(0, 19)).getTime() >= fromDate.getTime() && new Date(obj.date.substring(0, 19)).getTime() <= toDate.getTime()

                     
                    }
                )
            )

          

        }

    }, [toDate])


useffect(()=>{
if(fromDate!==null&&toDate!==null){
setReportData(
mainData.filter(
(obj)=>{
log(“db date new:”,新日期(obj.date.substring(0,19)))
日志(“选择的fromDate:,fromDate”)
日志(“所选toDate:”,toDate)

返回新日期(obj.Date.substring(0,19)).getTime()>=fromDate.getTime()&&new-Date(obj.Date.substring(0,19)).getTime()由于只想按日期过滤而忽略时间,可以执行以下操作:

 useEffect(() => {
        if (fromDate !== null && toDate !== null) {
        setReportData(
            mainData.filter(obj => {
                    return new Date(obj.date.substring(0, 19)).getTime() >= new Date(fromDate.getFullYear(), fromDate.getMonth(), fromDate.getDate(), 0, 0, 0).getTime() 
                    && new Date(obj.date.substring(0, 19)).getTime() <= new Date(toDate.getFullYear(), toDate.getMonth(), toDate.getDate(), 23, 59, 0).getTime()

                 
                }
            )
        )
    }

}, [toDate])

另一种方法是使用包含时间的日期初始化日期选择器的值,例如
2020-07-25T00:00:01
,然后选择的每个日期都将“携带”这一次。

在您的示例中,
选择的日期:Sat Jul 25 2020 22:46:00
大于
db date new:Sat Jul 25 2020 16:44:43
,因此不应包括此日期:)是的,这就是问题所在。这是因为时间对吗?