如何使用过滤器检查对象是否在日期范围内?

how to check if object is in date range using filter?

我正在尝试检查当前过滤器对象是否在日期范围内并基于数据 return 布尔值,如果数据不在日期范围内,则应过滤掉该对象。使用下面的代码,它总是 returning 数据。知道实现此任务的错误或正确方法是什么吗?

main.ts

 function mapResponse() {
    const _startDate = "2018-12-15";
    const _endDate = "2019-01-15";
    _startDate = moment.utc(_startDate || twentyfourMonthsAgo, ["YYYY-MM-DD",  "MM-DD-YYYY", "MM/DD/YYYY"]).format("YYYY-MM-DD");
    _endDate = moment.utc(_endDate || today, ["YYYY-MM-DD",  "MM-DD-YYYY", "MM/DD/YYYY"]).format("MM/DD/YYYY");
    const response = [
            {
                rxNumber: "15139",
                rxIssueDate: "",
                fillDate: "2019-01-03",
                quantityRemaining: "3",
                prescribedNoOfRefills: "3",
                currentFillNumber: "0"
            },
            {
                rxNumber: "16131",
                rxIssueDate: "",
                fillDate: "2019-12-03",
                quantityRemaining: "3",
                prescribedNoOfRefills: "3",
                currentFillNumber: "0"
            }
        ]

        response = response.filter(
            function renameSpecialtyAttributesToPBM(specialtyRx: any) {
                const mappedRx = specialtyRx as Partial < any > ;

                const dateFilter = checkDateRange(_startDate, _endDate, specialtyRx.fillDate);

                if (!dateFilter) {
                    return false;
                }

                mappedRx.fillDate = specialtyRx.fillDate;

                mappedRx.refillEligible = !!mappedRx.refillStatusText;
                mappedRx.renewEligible = !!specialtyRenewStatus;

                return true;
            });


    }
      return response;
}

checkDateRange.ts

function checkDateRange(startDate: any, endDate: any, fillDate: RxDetailsEntity): boolean {
    if (fillDate > startDate && fillDate < endDate) {
        return true;
    }

    return false;
}

mapResponse 的预期输出应该是

response = [
                {
                    rxNumber: "15139",
                    rxIssueDate: "",
                    fillDate: "2019-01-03",
                    quantityRemaining: "3",
                    prescribedNoOfRefills: "3",
                    currentFillNumber: "0"
                }]

将 dateStrings 转换为实际的日期对象。然后在这些日期执行 getTime() 以获得 unix 时间戳编号。现在您应该能够计算其中一个日期是否在开始日期和结束日期之间

看起来您正在将字符串日期传递给检查日期函数,并将它们与少于 than/greater 的运算符进行比较,这就是问题所在。

Moment 或较新的 Luxon 有比较实用的方法来比较日期,但如果你想用老式的方法来做,你可以这样做:

checkDateRange(startDateString: string, endDateString: string, fillDateString: string): boolean {
    const startDate = new Date(startDateString).getTime();
    const endDate = new Date(endDateString).getTime();
    const fillDate = new Date(fillDateString).getTime();
    if (fillDate > startDate && fillDate < endDate) {
        return true;
    }

    return false;
}

本机的 getTime() 方法 javascript Date class returns 自 unix 纪元以来日期的微秒数。这样您就可以用数字比较日期。

如果你已经有时刻日期,你可以时刻的isBetween方法:

const fillDate = moment(fillDateString).utc();
return fillDate.isBetween(_startDate, _endDate);