0

i am trying to check if current filter object falls into date range and based on data return boolean , if it data is not in date range that object should be filtered out. with below code its always returning the data. any idea what is implemented wrong or correct approach to achieve this task ?

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

expected output from mapResponse should be

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

2 Answers 2

1

Looks like you are passing string dates to your check dates function and comparing them with less than/greater than operators, which is where the problem is.

Moment or the newer Luxon have great utility methods for comparing dates, but if you want to do it the old-fashioned way, you could do it like this:

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

the getTime() method of the native javascript Date class returns the microseconds of the date since the unix epoch. This way you can compare dates numerically.

If you already have moment dates, you can moment's isBetween method:

const fillDate = moment(fillDateString).utc();
return fillDate.isBetween(_startDate, _endDate);
Sign up to request clarification or add additional context in comments.

2 Comments

i updated my question i am actually using moment for formatting dates , can you please help to compare using moments ?
ok thanks my fillDate is also moment utc so i would just return return fillDate.isBetween(_startDate, _endDate) in checkDateRange method
1

Convert the dateStrings to an actual date object. Then do getTime() on those dates to get the unix timestamp number. Now you should be able calculate whether it one of the dates is between a start and end date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.