• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1407
  • Last Modified:

Find date closest to todays date from list

How can I find the closest date to todays date from a combo box with multiple varying dates in?
0
Terry Rogers
Asked:
Terry Rogers
  • 4
  • 3
  • 2
1 Solution
 
Mortaza DoulatyCommented:
You can calculate the difference of each date in combo box with today's date, then sort the list ascending and get the closet date.

If you need, please let me know to post a code snippet.
0
 
Terry RogersIT Senior EngineerAuthor Commented:
A code snippet will be helpfull as am still new to VB.NET programming! :)
0
 
AmitGKCommented:
// Store current datetime in a variable
DateTime currentDateTime = DateTime.Now;
// Declare variable to store combobox date
DateTime ComboDateTime;
DateTime closestDateTime;

int prevDiff;
int currentDiff;
int i;
TimeSpan span;
prevDiff = 0;
//Loop thru combo box items
for (i = 0; i <= ComBoBox.Items.Count -1
; i++) {
// Populate the variable to combo box date
ComboDateTime = ComboBox.Items(i)
currentDiff = ComboDateTime.Subtract ( currentDateTime );
if (currentDiff < prevDiff)
{   closestDateTime = ComboDateTime.Items(i);
   prevDiff = currentDiff;
}
}
// variable closestDateTime has the closest date now
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
Mortaza DoulatyCommented:
Here is the code snippet:

        Dim closetDayIndex As Integer = -1
        Dim daysCount As Integer = System.Int32.MaxValue
        Dim dtc As New DateTimeConverter
        Dim dt As DateTime

        For index = 0 To Me.ComboBox1.Items.Count - 1
            dt = dtc.ConvertFromString(Me.ComboBox1.Items(index))
            If ((DateTime.Now - dt).TotalDays < daysCount) Then
                daysCount = (DateTime.Now - dt).TotalDays
                closetDayIndex = index
            End If
        Next

        MessageBox.Show("Closet day: " + Me.ComboBox1.Items(closetDayIndex))

Open in new window

0
 
Mortaza DoulatyCommented:
Also note that DateTimeConverter is in 'System.ComponentModel' namespace, so do not remember to include that namespace.
0
 
AmitGKCommented:
Please replace the following line
currentDiff = ComboDateTime.Subtract ( currentDateTime );

in my previous post with the following

span = ComboDateTime.Subtract ( currentDateTime );
currentDiff = span.Seconds;
if (currentDiff < prevDiff)
0
 
Mortaza DoulatyCommented:
AmitGK, please note that Terry is using VB .NET, not C#...
0
 
Terry RogersIT Senior EngineerAuthor Commented:
Thanks! :)
0
 
AmitGKCommented:
Mortaza_doulaty, fine noted man ;).
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now