<$BlogRSDURL$>

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:


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

This page is powered by Blogger. Isn't yours?