Site Feeds
Books
- The Elegant Universe (currently reading)
- The Dark Tower: Wolves of the Calla
- The Dark Tower: Wizard and Glass
- The Dark Tower: The Waste Lands
Blogroll
Useful Links
- Google News
- Artima Articles
- Microsoft Newgroups
- Scott Hanselman's Ultimate Developer and Power Users Tools List
Fun Links
Archives
A splat of all my blathering.
Monday, April 12, 2004
Any given Monday?
I was recently tasked with finding out the calendar date of of the first day of any week. I came up with the following code:
I have run it through a test that uses all 7 days as the first day of the week and calculates the date for 10,000 days. Upon spot checks of the results it seems to work. I have only tested it with the EN-us culture. I don't know how it works with other cultures.
Does anyone know of a different\better way or see any problems with this?
Posted at 4/12/2004 05:36:00 PM |
DateTime GetFirstDayOfWeekDate(DateTime reference, DateTimeFormatInfo dtfi)
{
Calendar cal = dtfi.Calendar;
DayOfWeek dow = cal.GetDayOfWeek(reference);
DateTime firstDayOfWeek = dtReference.AddDays((((int)dtfi.FirstDayOfWeek - (int)dow) - 7) % 7);
return firstDayOfWeek;
}
I have run it through a test that uses all 7 days as the first day of the week and calculates the date for 10,000 days. Upon spot checks of the results it seems to work. I have only tested it with the EN-us culture. I don't know how it works with other cultures.
Does anyone know of a different\better way or see any problems with this?
Posted at 4/12/2004 05:36:00 PM |
Comments:
Post a Comment