Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System constant for the number of days in a week (7)

Can anyone find a constant in the .NET framework that defines the number of days in a week (7)?

DateTime.DaysInAWeek // Something like this???

Of course I can define my own, but I'd rather not if it's somewhere in there already.

Update:

I am looking for this because I need to allow the user to select a week (by date, rather than week number) from a list in a DropDownList.

like image 591
Richard Ev Avatar asked Jan 07 '09 09:01

Richard Ev


4 Answers

You could probably use System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames.Length.

like image 70
Matt Hamilton Avatar answered Nov 01 '22 04:11

Matt Hamilton


I think it's ok to harcode this one. I don't think it will change any soon.

Edit: I depends where you want to use this constant. Inside the some calendar related algorithm it is obvious what 7 means. On the other hand sometimes named constant make code much more readable.

like image 9
Jakub Šturc Avatar answered Nov 01 '22 05:11

Jakub Šturc


Try this:

Enum.GetNames(System.DayOfWeek).Length
like image 5
tanathos Avatar answered Nov 01 '22 05:11

tanathos


If you look at the IL code for Calendar.AddWeeks you will see that Microsoft itself uses a hardcoded 7 in the code.

Also the rotor source uses a hardcoded 7.

Still, I would suggest to use a const.

like image 3
GvS Avatar answered Nov 01 '22 06:11

GvS