473,485 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

21 New Member
Hi,
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;
TD=Field1.ValueAsDateTime;
NTD=Field2.ValueAsDateTime;
if (TD < NTD)
{
min = TD;
TD = NTD;
return min;
}
}
MessageBox.Show(min.ToString());


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.

Thanks
Oct 8 '07 #1
4 5352
Plater
7,872 Recognized Expert Expert
Conver them to DateTime objects. DateTime objects support <, >, = operators so you can treat them as you would an integer
Oct 8 '07 #2
cathy25
21 New Member
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
Plater
7,872 Recognized Expert Expert
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.
  13.  
Oct 8 '07 #4
cathy25
21 New Member
Thank you very much.
It is working now.
I was missing that logic.

Thanks
Oct 8 '07 #5

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

Similar topics

9
1891
by: Simon Harris | last post by:
Hi All, Ok - I'll confess from the start, this is more about application logic that ASP, being an ASP programmer, I guessed you people might be able to help! :) I have built a room bookings...
1
2812
by: Teresa | last post by:
I need to set criteria in a query to pull all transfer dates that are between 1 year before and 6 mos before Then I need to pull all service dates that are within six months of that...
2
1234
by: Randy | last post by:
How do you do a find on a range of Dates in a Date field from the find dialog box?
17
2985
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
18
38202
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
9
2234
by: newtophp2000 | last post by:
I need to add the ability for the users to select a date on one of our web pages. The examples I was given by some users were in JavaScript. Is there a free date picker that I can use? I saw one...
67
7598
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
23
13995
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
13
12993
newnewbie
by: newnewbie | last post by:
I have an Access database that I upload a data extract intoto daily. I want to create a query that will give me a list of dates that no data can be found for. E.g. there are no records created on the...
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.