Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.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
Arrays 如何在Reactjs中过滤从一个日期到另一个日期的数组_Arrays_Reactjs_Filter_Datepicker - Fatal编程技术网

Arrays 如何在Reactjs中过滤从一个日期到另一个日期的数组

Arrays 如何在Reactjs中过滤从一个日期到另一个日期的数组,arrays,reactjs,filter,datepicker,Arrays,Reactjs,Filter,Datepicker,我有一个对象数组“mainData”,如下所示: 现在,我允许用户使用mui日期选择器选择开始日期和结束日期。这就是我接收值的方式: fromDate: Sat Jul 25 2020 11:43:00 toDate: Sat Aug 08 2020 11:43:00 现在我想筛选从这个日期到那个日期的数组,包括起始日期和结束日期。我试图这样做,但它只返回一个空数组。我已经将代码放在useEffect中,它在每次toDate更改时都会运行,而且我还使用Moment使两个日期的格式相同: us

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

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

fromDate: Sat Jul 25 2020 11:43:00
toDate: Sat Aug 08 2020 11:43:00
现在我想筛选从这个日期到那个日期的数组,包括起始日期和结束日期。我试图这样做,但它只返回一个空数组。我已经将代码放在useEffect中,它在每次toDate更改时都会运行,而且我还使用Moment使两个日期的格式相同:

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

                        return Moment(obj.date).format("DD MMM yyyy") >= Moment(fromDate).format("DD MMM yyyy") && Moment(obj.date).format("DD MMM yyyy") <= Moment(toDate).format("DD MMM yyyy")
                    }


                )
            )

           

        }

    },[toDate])

对象的日期属性可以直接解析为
date
对象。因此,您可以使用
getTime
。 此外,过滤器还返回
Date
对象

因此,您可以将代码更改为

 useEffect( () => {
    if (fromDate !== null && toDate !== null) {
        setReportData(
            mainData.filter(
                (obj) =>{
                    return new Date(obj.date).getTime() >= fromDate.getTime() && new Date(obj.date).getTime() <= toDate.getTime()
                }
            )
        )
    }

},[toDate])
useffect(()=>{
if(fromDate!==null&&toDate!==null){
setReportData(
mainData.filter(
(obj)=>{

返回新日期(obj.Date).getTime()>=fromDate.getTime()&&newdate(obj.Date).getTime()我们也可以使用矩。js。通过将矩转换为预期格式

在上述情况下,我们有
2020年7月25日星期六11:43:00

矩使用
llll
提供了区域设置支持格式,这与 对于这个,用法如下。 在
if
语句之后的顶部某处初始化格式常量

const format = 'llll';
只需将filter return语句替换为:

return Moment(obj.date, format).unix() >= Moment(fromDate, format).unix() && Moment(obj.date, format).unix() <= Moment(toDate, format).unix()

返回时刻(obj.date,format).unix()>=时刻(fromDate,format).unix()和时刻(obj.date,format).unix()好的,哇,这很有效,但有一个小问题,它没有返回toDate值上的对象。虽然数组中在该日期有on对象。你能告诉我为什么吗?哪个对象和什么
toDate
?例如,我选择了fromDate 25和toDate 9,它应该返回一个大小为6的数组,但它返回一个array的大小为5。您可以在我的qs中引用mainData数组。好的,然后在您的if语句后尝试
控制台。log
查看每个属性有什么,以及它与过滤器的比较方式。hmmm我认为这与时区有关,因为例如
新日期(“2020-07-25T16:44:43.000Z”)
将返回不同的日期对象(和时间)这取决于您的时区,因此可能是正确的,因为该对象似乎在您的搜索条件中包含时间,但转换为本地时区时,该时间将超出您的日期条件边界。当我尝试匹配单个日期时,这不起作用。这似乎是稍后编辑的问题@Aldor。您能帮助吗请允许我提供
oneDate
的格式输出日期,以便我在上面的答案中加起来,使其对功能读取有用。
 useEffect( () => {
    if (fromDate !== null && toDate !== null) {
        setReportData(
            mainData.filter(
                (obj) =>{
                    return new Date(obj.date.substring(0, 19)).getTime() >= fromDate.getTime() && new Date(obj.date.substring(0, 19)).getTime() <= toDate.getTime()
                }
            )
        )
    }

},[toDate])
const format = 'llll';
return Moment(obj.date, format).unix() >= Moment(fromDate, format).unix() && Moment(obj.date, format).unix() <= Moment(toDate, format).unix()