By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,971 Members | 616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,971 IT Pros & Developers. It's quick & easy.

how to find first day among given dates

P: 13
I have selected some list of valid dates to a label.
now i need to find first day among the given dates from label contrl

i got dates from calender control by clicking for entire week..

that is i have dates from sunday to saturday and I have to find which date is sunday......
Feb 12 '09 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 750
try this
Expand|Select|Wrap|Line Numbers
  1. DateTime now = DateTime.Now;
  2.             List<DateTime> allDates = new List<DateTime>();
  3.             allDates.Add(now);
  4.             allDates.Add(now.Subtract(new TimeSpan(0,0,20,20,0)));
  6.             allDates.Sort();
Feb 12 '09 #2

Expert 2.5K+
P: 3,525
This would be a good example of not planning ahead. You coded, before you planned. Two words: "Software engineer" - meaning to engineer the software. A person wouldn't build a building by nailing together a bunch of wood, then two days in start thinking about how they are going to accommodate a door and window. Same in this field.

Enough with the scolding...
Based on your question I'm guessing you did exactly what you said: You added dates as text into a label. Now you are stuck because you don't have any dates to work with, only text. It is possible to convert text back into DateTime (do some reading about TryParse), but it would be better to re-engineer what your application is doing.
Tip 1: Never throw something away until you are sure you are done with it.
Tip 2: Remember to keep your data, and your user interface seperate in your mind.

In this case, as you take in dates from your Calendar control be sure to add them to a List<> or Array of DateType objects just as DeepBlue has indicated. This is the data you will actually use. If you also want to add them to a text box, list control, label or some other UI for your user that's cool too, but the UI is just for the user and not for your program to think with.

Now that you have a list/array of DateTime objects you can work through them for whatever purpose you want. Use a 'for' loop to check each item in the list/array. Is it a Sunday date? Is it after 2 pm? Is it before or after your target date? and so on.
Feb 12 '09 #3

Expert 100+
P: 750
extremely good "expert" advice tlhintoq... thanks a lot ...
pchaitanya, do remember as tlhintoq pointed out "the UI is just for the user and not for your program to think with ".. it will be better to keep selected dates as date in some list for further processing ....
Feb 13 '09 #4

Post your reply

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