Formula from dates to working hours

hello everyone!!

I’ve the following situation with the formula “From dates to working hours” with the scenario: get working hours between two dates with times in a 08:00-17:00 (8AM-9PM) work schedule.

((NETWORKDAYS(DATEVALUE({item’s Fecha de creación}), DATEVALUE({item’s Fecha de Solución})) - 1) * (TIMEVALUE(“17:00”) - TIMEVALUE(“08:00”)) + IF(NETWORKDAYS({item’s Fecha de Solución}, {item’s Fecha de Solución}), MEDIAN(MOD(TIMEVALUE({item’s Fecha de Solución}), 1), TIMEVALUE(“17:00”), TIMEVALUE(“08:00”)), TIMEVALUE(“17:00”)) - MEDIAN(NETWORKDAYS(DATEVALUE({item’s Fecha de creación}), DATEVALUE({item’s Fecha de creación})) * MOD(TIMEVALUE({item’s Fecha de creación}), 1), TIMEVALUE(“17:00”), TIMEVALUE(“08:00”))) * 24

I create and configure the above formula in the following automation on a monday.com board:

The formula is working, but it’s generating the following inconsistency with this scenarios:

The formula doesn’t take the working hours in the afternoon, when a ticket is closed on the same day:
error 1
error 2

but if I update to the next working day, in the morning, it updates the correct hours in the column Hr de trabajo:
correcto 1
correcto 2
I appeal to the great collaboration and expertise : I need your help on how to adjust the formula so that I can take the hours between two dates in the afternoon in the same day:

forma correcta 1
forma correcta 2

Thank!!!