views:

575

answers:

9

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.

A: 

I don't believe there is one. TimeSpan defines constants for the number of ticks per milli/second/minute/hour/day, but nothing at the level of a week.

I ran a query across the standard libraries for symbols (methods/constants/fields/etc) containing the word 'Week'. Nothing came back. FYI, I ran this query using ReSharper.

Drew Noakes
+5  A: 

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.

Jakub Šturc
Still better to use a named constant so that anyone reading the code knows what it is and doesn't have to wonder whether it's just the developer's lucky number.
Michael Borgwardt
Also, it could change if you want to calculate something based on a 5 day working week instead of all 7 days?
RSlaughter
+4  A: 

Try this:

Enum.GetNames(System.DayOfWeek).Length
tanathos
That'll work, but keep in mind that the MSDN documentation for the DayOfWeek enum says that it's only valid in cultures who have a seven-day week.
Matt Hamilton
Having said that, I'm not sure that there are any cultures that don't have a seven day week! :)
Matt Hamilton
@Matt Hamilton : lol!
Mitch Wheat
http://en.wikipedia.org/wiki/Week
Hamish Grubijan
+8  A: 

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

Matt Hamilton
A: 

GregorianCalendar has AddWeeks(1) which will add 7 days to a date.

MSalters
Could you add an example showing how to use this?
Richard Ev
+2  A: 

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.

GvS
You can't tell from the IL whether it is a literal 7 or a constant defined elsewhere, as constants are resolved at compile time.
Greg Beech
OK, but there is also no constant definition. Looked it up in rotor.
GvS
A: 

Just a silly question... Are there weeks bigger or smaller then 7 days? I can't imagine why you would need this...

Sergio
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. So I need to build up my list of dates.
Richard Ev
A: 

Do you mean calendar weeks or just common weeks?

Obviously, there are calendar weeks that might be shortrer than seven days. The last calendar week of the year is usually shorter, and depending on your definition of calendar week, the first week might be shorter as well.

In that case, I'm afraid you will have to roll out your own week length function. It's not really hard to do with the DateTime class, I did it before, if you need more help let me know.

DrJokepu
Common weeks.....7 :-)
Richard Ev
Just create a constant in your class then if you really want to avoid hardcoding numbers, even well-known ones.
DrJokepu
A: 

I'm not sure what exactly you're looking for, but you can try DateHelper (CODE.MSDN). It's a library I put together for typical date needs. You might be able to use the week methods or the List methods. method list

Tony Basallo