Solved

# Find date closest to todays date from list

Posted on 2010-09-02
1,358 Views
How can I find the closest date to todays date from a combo box with multiple varying dates in?
0
Question by:Terry Rogers
• 4
• 3
• 2

LVL 12

Expert Comment

ID: 33584716
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

LVL 1

Author Comment

ID: 33584728
A code snippet will be helpfull as am still new to VB.NET programming! :)
0

LVL 4

Expert Comment

ID: 33584795
// 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

LVL 12

Accepted Solution

Mortaza Doulaty earned 500 total points
ID: 33584804
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))
``````
0

LVL 12

Expert Comment

ID: 33584816
Also note that DateTimeConverter is in 'System.ComponentModel' namespace, so do not remember to include that namespace.
0

LVL 4

Expert Comment

ID: 33584852
currentDiff = ComboDateTime.Subtract ( currentDateTime );

in my previous post with the following

span = ComboDateTime.Subtract ( currentDateTime );
currentDiff = span.Seconds;
if (currentDiff < prevDiff)
0

LVL 12

Expert Comment

ID: 33584868
AmitGK, please note that Terry is using VB .NET, not C#...
0

LVL 1

Author Closing Comment

ID: 33584968
Thanks! :)
0

LVL 4

Expert Comment

ID: 33585006
Mortaza_doulaty, fine noted man ;).
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.