【问题标题】:how to check if object is in date range using filter?如何使用过滤器检查对象是否在日期范围内?
【发布时间】:2019-01-22 17:41:16
【问题描述】:

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

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"
                }]

【问题讨论】:

    标签: javascript angular typescript date-range


    【解决方案1】:

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

    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;
    }
    

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

    如果你已经有moment日期,你可以使用moment的isBetween方法:

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

    【讨论】:

    • 我更新了我的问题,我实际上是在使用时刻来格式化日期,你能帮忙比较一下使用时刻吗?
    • 好的,谢谢我的 fillDate 也是时刻 UTC 所以我会在 checkDateRange 方法中返回 return fillDate.isBetween(_startDate, _endDate)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2010-11-01
      相关资源
      最近更新 更多