Datetime closest to

Hello

I am creating a library that takes an in-argument for desired arrival date and time - eg. “03-07-2024” and “08:00”.

This input is forwarded to an API, that returns available trips and tickets.
This API returns about 5/6 available trips around the desired travel time, and now I need to return the “closest available trip” based on the desired arrival.

The API-result could be something like this:

  • Trip 1: 02-07-2024 21:54
  • Trip 2: 02-07-2024 22:05
  • Trip 3: 03-07-2024 04:45
  • Trip 4: 03-07-2024 07:43
  • Trip 5: 03-07-2024 10:46

My plan is to extract these results in a List or Datatable, but how do I find the closest one?
Bear in mind that the results can be different dates.

Regards,
Soren

@SorenB,

As you are going to get data from API so I’m assuming you will get it in JSON format.

You can deserialize the JSON into list using Deserialize JSON Activity and return the output.

Thanks,
Ashok :slight_smile:

Hello Ashok

I already do this procedure.
However the API returns several results, and from time to time the “correct/closest” match might be result 1,2,3,4,5 etc.

So the question is how to compare date and time from the trip-results with the desired date and time.

Why dont you subtract the desired travel time with the result. the result with least difference will be the closest

edit:

closestTrip = trips.OrderBy(Function(trip) Math.Abs((trip - DateTime.Parse(inputDate)).Ticks)).First()

you can try using the linq trips is a list which you will have to convert from your json

Hi @SorenB

Closest date means Latest date in the list right.

If yes, then store all the data in a List of String datatype variable. Check the below steps,
→ Use the Assign activity to assign the values to a List as below,

- Assign -> List_Values = New List(Of String) From {"Trip 1: 02-07-2024 21:54","Trip 2: 02-07-2024 22:05","Trip 2: 03-07-2024 04:45","Trip 3: 03-07-2024 07:43","Trip 4: 03-07-2024 10:46"}

- Assign -> RecentDateTime = List_Values.OrderByDescending(Function(X) CDate(System.Text.RegularExpressions.Regex.Match(X.ToString, "(?<=Trip.*\:.*)[\d\s\-\:]+").Value)).First

Check the below workflow for better understanding,

Hope it helps!!

Please share with us the JSON as Text File. We see a chance to do it more direct over JSON

@SorenB,

Ok so the result’s you are storing in List/Array? If yes, you can compare it with the input data using LINQ.

Thanks,
Ashok :slight_smile:

Hello all

In the initial shown example Trip 4 (03-07-2024 07:43) would be closest to the desired arrival time (03-07-2024 08:00).

dd-MM-yyyy

JSON would be like this:

"Trip":[
{"id":"Trip1",
"date":"02-07-2024",
"time":"21:54"},
{"id":"Trip2",
"date":"02-07-2024",
"time":"22:05"},
{"id":"Trip3",
"date":"03-07-2024",
"time":"04:45"},
{"id":"Trip4",
"date":"03-07-2024",
"time":"07:43"},
{"id":"Trip5",
"date":"03-07-2024",
"time":"10:46"}
]

I have provided a LINQ expression in the above post. Does it help you? @SorenB

Hello @mkankatala

The trip with the date and time “03-07-2024 10:46” is not the correct one, so I am afraid not.

Perfect, so we can refer now to the details, and also see that the JSON values are not exact the same as the initial shared input sample. Such details are very important.

JSON deserialization:
grafik

grafik
myJObject

So we can check how much we can grab by out oft the box conversions (we recommend to confirm, that day, month position are crosschecked, where exact is the day?)

grafik
TimeSpan is Failing, but can be handled in that way:
grafik

So we can use LINQ and order to the relevant JObject

For sure we can also adapt and/or shift to Query Syntax

then lets rephrase the requirement

Can you confirm

closest to the 8h Arrival day, but cannot be later?

Hello Peter

My problem is not with extracting the data from the API :upside_down_face:
The data could be in whatefter format you like.

My question is how do I compare a date and time (DateTime) to eg. 5/6 other DateTimes and find the closest match?
The date could be forward or backward in time - I just need the closest match to my input.

And just to clarify the initial example.

If my input is “03-07-2024 08:00” then the closest match is “03-07-2024 07:43” (17 minutes apart).

grafik
grafik

myJOFiltered =

(From jo In myJObject("Trip").Values(Of JObject)
Let dp = jo("date").Value(Of DateTime)
Let dt = TimeSpan.Parse(jo("time").toString)
Where dp = myFilterDate.Date
Where dt <= myFilterDate.TimeOfDay
Order By dt
Select jof=jo).Last()

and for sure we can adapt

Would this also work if lets say that the suggested trips are different dates from input.

Example:

  • Input: 03-07-2024 08:00

API results:

  • Trip 1: 02-07-2024 22:54
  • Trip 2: 04-07-2024 00:01

requires adapations (initial requierement was different stated) let us do the following
order all given dates from closest to farest from the input, but filter out any dates later as the given filter date (which is a variable and is therefore dynamizable)

Assign Acitvity:

arrJOFilterResult | Array Of JObject =

(From jo In myJObject("Trip").Values(Of JObject)
Let sd = jo("date").Value(Of String)
Let st = jo("time").Value(Of String)
Let dp = CDate(sd + " " + st)
Where dp <= myFilterDate
Order By dp Descending
Select jof=jo).ToArray

And with arrJOFilterResult.First(), arrJOFilterResult.Take(2)… you can control on how you want to process the result

was working with the adapted LINQ
grafik

extending the requirements and it solution to such a case

Example:

Input: 03-07-2024 08:00

API results:

Trip 1: 02-07-2024 22:54
Trip 2: 03-07-2024 07:47
Trip 3: 03-07-2024 08:03    
Trip 4: 04-07-2024 05:10

So, trip 3 closet to input but after 8:00
trip 2,1 before input

Using the Ticks and Math.Abs can help to handle such cases. But we would feel that a threshold how far after input date and/or prioritized ordering (what before, what after) can maybe also adress the needs

Let us know if there is interest in such approaches as well

Hello @ppr

I fail to see how this requirement is different from initially stated.
I literaly wrote:

This API returns about 5/6 available trips around the desired travel time, and now I need to return the “closest available trip” based on the desired arrival.

Bear in mind that the results can be different dates.

I would rather that a List or Datatable was used in stead of this overly complicated runthrough of the JSON.
The initial question was not how to read/process the API-results but just how to find the DateTime closest among several other.

My plan is to extract these results in a List or Datatable, but how do I find the closest one?

Regards
Soren