?
Solved

Find date closest to todays date from list

Posted on 2010-09-02
9
Medium Priority
?
1,384 Views
Last Modified: 2012-05-10
How can I find the closest date to todays date from a combo box with multiple varying dates in?
0
Comment
Question by:Terry Rogers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 12

Expert Comment

by:Mortaza Doulaty
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

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

Expert Comment

by:AmitGK
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 12

Accepted Solution

by:
Mortaza Doulaty earned 2000 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))

Open in new window

0
 
LVL 12

Expert Comment

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

Expert Comment

by:AmitGK
ID: 33584852
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
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 33584868
AmitGK, please note that Terry is using VB .NET, not C#...
0
 
LVL 1

Author Closing Comment

by:Terry Rogers
ID: 33584968
Thanks! :)
0
 
LVL 4

Expert Comment

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

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question