Solved

Find date closest to todays date from list

Posted on 2010-09-02
9
1,363 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 12

Accepted Solution

by:
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))

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# LINQ ForEach() question 6 54
Release Dynamically Allocated Memory in C# 3 38
vb.net and creating a class 5 20
PowerShell:  Column widths won't expand 3 17
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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