Solved

Find date closest to todays date from list

Posted on 2010-09-02
9
1,344 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now