470,864 Members | 1,834 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,864 developers. It's quick & easy.

How to find least date in a set of dates in C#

I am new to programming in C#. Now, I am struck with a logic.
I have a set of date fields (DateTime picker controls) . I need to find out the least date in those date fields.
I tried the following logic.

for (i=1; i < 20; ++i)
int j=i+1;
string Field1 = tdField + i.ToString();
string Field2 = tdField + j.ToString();
DateTime TD, NTD;
min = DateTime.Now;
if (TD < NTD)
min = TD;
return min;

This code is executing fine at runtime, but it is not giving me the correct least date. It is executing for 20 times (as mentioned in for loop) and it is giving me the least number in the 19th and 20th dates. I am sure this code will do like that only, but what is the change I need to do for getting the least date in the set of 20 dates.
I am really struck with this logic from 2 days.
I would appreciate any help in this regard.

Oct 8 '07 #1
4 5134
7,872 Expert 4TB
Conver them to DateTime objects. DateTime objects support <, >, = operators so you can treat them as you would an integer
Oct 8 '07 #2
I am not getting any error at the code. But I think the logic will be different for my requirement. Please help me finding out the logic.
Oct 8 '07 #3
7,872 Expert 4TB
The "least" date means oldest yes?

Expand|Select|Wrap|Line Numbers
  1. DateTime oldestdt = DateTime.MaxValue
  2. for(int i=0;i<20;i++)
  3. {
  4.    string Field1 = tdField + i.ToString();
  5.    DateTime TD;
  6.    TD=Field1.ValueAsDateTime;
  7.    if ( TD<oldestdt)
  8.    {
  9.       oldestdt=TD;
  10.    }
  11. }
  12. //at the end of the loop "oldestdt" will be the oldest date time.
Oct 8 '07 #4
Thank you very much.
It is working now.
I was missing that logic.

Oct 8 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Simon Harris | last post: by
17 posts views Thread by Justin Emlay | last post: by
18 posts views Thread by dfetrow410 | last post: by
9 posts views Thread by newtophp2000 | last post: by
67 posts views Thread by PC Datasheet | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.