I am looping through a list of DateTime variables and have got the .ToUniversalTime variable. I want to find out from the list which is the closest one approaching Now.ToUniversalTime but am having difficulty trying to work it out

I had it looping through like this but this isn't much good as I only want to return the one that is the closest approaching to the system time.

If TodaysCard(i).eventDate.ToUniversalTime < Now.ToUniversalTime Then

Any guidance would be greatly appreciated.
Thanks,

Recommended Answers

All 3 Replies

I am looping through a list of DateTime variables and have got the .ToUniversalTime variable. I want to find out from the list which is the closest one approaching Now.ToUniversalTime but am having difficulty trying to work it out

I had it looping through like this but this isn't much good as I only want to return the one that is the closest approaching to the system time.

If TodaysCard(i).eventDate.ToUniversalTime < Now.ToUniversalTime Then

Any guidance would be greatly appreciated.
Thanks,

Hi,

Try this:

Dim tempUTC as Date = CDate("01/01/2010")
tempUTC = tempUTC.ToUniversalTime
'Add your loop code here...
If TodaysCard(i).eventDate.ToUniversalTime < Now.ToUniversalTime Then
If TodaysCard(i).eventDate.ToUniversalTime > tempUTC Then tempUTC = TodaysCard(i).eventDate.ToUniversalDate
Else
'Exit Loop
End If
'Return tempUTC

You may or may not want to jump out of the loop depending on whether your list is sorted of course (and I'm not sure how you're looping through the list either - so it's not in the example, sorry).

Something along these lines should do what you want though.

Chris.

That looks great Chris.
Thank you.

:)

You're welcome, glad I could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.