Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8471163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:43:15+00:00 2026-06-10T16:43:15+00:00

I am currently using the function below to return the nearest date from a

  • 0

I am currently using the function below to return the nearest date from a list of dates to a date(today). My problem is, the function returns the closest date regardless of it being the past or the future of todays date. How can I change this code so there is an option to return the nearest date AFTER today and the nearest date BEFORE today? It’s confusing the hell out of me.

Thanks a lot for your input.

Function GetNearestDate(ByVal source As IEnumerable(Of DateTime), ByVal target As DateTime) As DateTime
    Dim result As DateTime = Nothing
    Dim lowestDifference = TimeSpan.MaxValue

    For Each _date As DateTime In source

        If _date >= target Then
            Continue For
        End If

        Dim difference = target - _date

        If difference < lowestDifference Then
            lowestDifference = difference
            result = _date
        End If
    Next

    Return result
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:43:17+00:00Added an answer on June 10, 2026 at 4:43 pm

    Seems like this is what you are looking for. you simply need to be able to pass something in to the function so it knows what you want. I chose an enum for explicitness. I also updated it to pass back a nullable date. This should correct your time issues, but you’ll need to account for null returned values.

    Public Enum DateCompare
        LessThanEqualTo
        GreaterThanEqualTo
    End Enum
    
    Public Function GetNearestDate(ByVal source As IEnumerable(Of DateTime), _
                                   ByVal target As DateTime, _
                                   ByVal dt As DateCompare) As Nullable(Of DateTime)
        Dim result As Nullable(Of DateTime) = Nothing
        Dim lowestDifference As TimeSpan = TimeSpan.MaxValue
        Dim difference As TimeSpan
    
        For Each _date As DateTime In source
            If dt = DateCompare.LessThanEqualTo And _date > target Then
                Continue For
            ElseIf dt = DateCompare.GreaterThanEqualTo And _date < target Then
                Continue For
            End If
    
            If target > _date Then
                difference = target - _date
            Else
                difference = _date - target
            End If
    
            If difference < lowestDifference Then
                lowestDifference = difference
                result = _date
            End If
        Next
    
        Return result
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the function below to disable SeSystemtimePrivilege on a token obtained from: OpenProcessToken(GetCurrentProcess(),
I am currently using the code below in a project date :: IO (Integer,Int,Int)
I am currently using below function to sanitize my $_POST and $_GET against SQL
I am currently using a function in my app's didFinishLaunchingWithOptions that retrieves a file,
I'm currently using the toprettyxml() function of the xml.dom module in a Python script
I am currently using the following code to update form data before submission. $('.countyx').change(function(){
Here is the code I'm currently using: $.ajax({ success: function(){ $('.post_container').append('test <br />'); }
I'm writing an application, and I'm currently using libcurl. The libcurl callback function works
Currently i am using DWS CreateFolder function to create folder on Sharepoint (using C#).
Currently, I am using this function: function tokenize( str ) local ret = {}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.