Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop through days of week, how?

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
GB
Hi all, I am developing a program which does quite a lot with days of the week so I am trying to develop a way to loop through days of the week. This is so that no matter who workes on development Monday will always be zero. I have considered Enum and came up with this.
Code:
enum DayNumber : byte
{
    MON = 0,
    TUE = 1,
    WED = 2,
    THU = 3,
    FRI = 4,
    SAT = 5,
    SUN = 6
 }

private DayNumber aDay
{
   get
   {
       return currentDay;
   }
}

private DayNumber currentDay;

But is there a better way? If this way is correct then how do loop through the days?

Thx T


Age is a consequence of experience
 
I wrote a large calendar piece a long time ago. I used DateTime and returned things like DateTime.DayOfWeek.

Then to loop through the week, I would say:

DateTime now = DateTime.Now;

for (int i = 0; i < 7; i++)
{
Console.WriteLine(now.AddDays(i).ToString());
}
 
thanks for the reply, yes that is how i do it at the moment, but i wanted to make it so that 0 would always be monday no matter who was writing the code. perhaps i should just leave as it is.

Age is a consequence of experience
 
Instead of defining your own enum, why not use the one that comes with the framework?

[tt]System.DayOfWeek[/tt]

And to print out the day title you'd use the DateTimeFormatInfo class:
Code:
DateTime dt = DateTime.Now;
DateTimeFormatInfo dtfi = CultureInfo.CurrentUICulture().DateTimeFormat;
Console.WriteLine(dt.ToString("ddd"), dtfi);
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
ok thx guys, i will have a think on this i think.

Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top