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#

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 5295
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

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

Similar topics

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...
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...
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?
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). ...
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
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...
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 ...
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...
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...
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...
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...
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...
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 ...
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...
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...
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=()=>{
by: lllomh | last post by:
How does React native implement an English player?
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.