Home  >  Q&A  >  body text

Need help building a method that returns the earliest date value

I'm refactoring a method for a legacy codebase I took over. This method accepts an object of predicted dates. When the forecast date is within 30, 40, or 60 days, the getTime() method should be used to convert, returning the earliest time.

Currently returns the earliest date of the object it receives, the problem is that it only compares with 2 dates, it returns a star date and only returns 1 of the 2 values ​​from the end date instead of comparing all 3 and Return the lowest .getTime() value

The following is the method to return the earliest date

const getEarliestRunout = (runout_dates = {}, dm1_type = '') => {
     try{
        const cur_year = new Date().getFullYear();

        const non_priority_sticker = dm1_type == cur_year + 1 || dm1_type == cur_year ? 'dm1' : 'dm2';
    
        return Object.entries(runout_dates).reduce((earliest,obj) => {            
            const key = obj[0].split('_')[0];

            const value = obj[1];
            
            if(value[`under_${env.STICKER_THRESH[0]}`] && key != non_priority_sticker) {
                return new Date(value[`under_${env.STICKER_THRESH[0]}`]).getTime() < earliest.val ? {val: new Date(value[`under_${env.STICKER_THRESH[0]}`]).getTime(), date: value[`under_${env.STICKER_THRESH[0]}`]} : {...earliest}
            }
            return earliest
        },{val: Infinity, date:''})
    }catch(e){
        console.error(`ERROR :: util.getEarliestRunout: ${e} - ${new Date()}`);
        return {val: Infinity, date: ''}
    }
}

The following are the variables used to call the method being used

const earliest_runout = getEarliestRunout({
 dm1_runouts: value.dm1_type == priority_sticker ? priorityRunouts : nonPriorityRunouts,
 dm2_runouts: value.dm2_type == priority_sticker ? priorityRunouts : nonPriorityRunouts,
 star_runouts: starRunouts
},value.dm1_type);

I expected this method to compare all dates, but it only compares 2. I'm pretty sure this is how I'm calling the method, but I'm not sure. I'm looking to resolve this issue further as I'm confused on how to obtain the results I'm looking for.

I'm sure I can rephrase this more clearly, but just to reiterate. This method accepts date values ​​(only 3 dates in my use case) and returns the earliest date using Math.min and getTime(). The result I receive is that it returns all date values ​​but only compares the asterisk value to one of dm1_runout or dm2_runout. It remains consistent across comparisons, returning only one or the other, but not both.

P粉521013123P粉521013123179 days ago1364

reply all(1)I'll reply

  • P粉949267121

    P粉9492671212024-04-05 00:13:51

    I solved my problem with the answer. I created an array of dates, ran each method through the Math.min method and pushed each value into the date array, then sorted the array to filter the NaNs with the empty string and return only the values ​​in Math.min Loop through each object's array matching the minimum value.

    reply
    0
  • Cancelreply