Calculating which object of a list has the closest time

0
Hi all,   Is there a way in a microflow or another way to take a list of objects which have different times and have Mendix calculate which is the closest time to your current time.   So for example Mendix would look at list of times like the image shown above and then if the time was 18:00 it would know the closest object in the list was 14:00.   Any help would be great thanks.   Kind regards Nathan
asked
2 answers
0

The straightforward way:

Loop over the list and at each iteration calculate the difference using millisecondsBetween($IteratorYourObjects, $currentDateTime)

The probably more efficient way:

- retrieve the list, with ‘Range: first’, xpath ‘Time’ < currentDateTime and sorting on Time descending.

- retrieve the list, with ‘Range: first’, xpath ‘Time’ > currentDateTime and sorting on Time ascending.

-  using millisecondsBetween to calculate which of the two is closest to currentDateTime.

answered
0

You can do it faster with an Xpath. Just retrieve the first object where the datetime field is < then the current date time with a descending sort on that same datetime field. The record you retrieve is the one.

Regards,

Ronald

 

answered