472,809 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

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

21
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 5295
Plater
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
cathy25
21
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 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.
  13.  
Oct 8 '07 #4
cathy25
21
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
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
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
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
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
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
9
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
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
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
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
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.