tags:

views:

332

answers:

4

I have a weird date rounding problem that hopefully someone can solve. My client uses a work week that runs from Monday through Sunday. Sunday's date is considered the end of the week, and is used to identify all records entered in a particular week (so anything entered last week would have a WEEKDATE value of '10/26/2008', which is Sunday's date).

One little twist is that users enter records for the previous week up until 11 AM on the Monday of the current week.

So I need a function that starts with DateTime.Now and returns the week-ending date (no time part) according to the rules above. Thanks for your help. I have a solution that works, but I'm too embarassed to post it.

Oh, and I can't use LINQ.

+3  A: 
public DateTime WeekNum(DateTime now)
{
 DateTime NewNow = now.AddHours(-11).AddDays(6);

 return (NewNow.AddDays(- (int) NewNow.DayOfWeek).Date);
}

public void Code(params string[] args)
{

 Console.WriteLine(WeekNum(DateTime.Now)); 
 Console.WriteLine(WeekNum(new DateTime(2008,10,27, 10, 00, 00)));
 Console.WriteLine(WeekNum(new DateTime(2008,10,27, 12, 00, 00)));
 Console.WriteLine(WeekNum(new DateTime(2008,10,28)));
 Console.WriteLine(WeekNum(new DateTime(2008,10,25)));


}

You may hard-code DateTime.Now instead of passing a DateTime object. It just made testing easier this way.

James Curran
DayOfWeek is Sunday = 0, Monday = 1 etc.?
MusiGenesis
That doesn't work. I just ran it and it gave me yesterday.
Joel
@Joel: Sorry, posted too quickly. Revised version should work.
James Curran
Thanks, James. I added .Date to the return line, so I only get the date part.
MusiGenesis
+4  A: 

This passes for me as well:

[Test]
public void Test()
{
   DateTime sunday = DateTime.Parse("10/26/2008");
   DateTime nextSunday = DateTime.Parse("11/2/2008");

   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/21/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/22/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/23/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/24/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/25/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/26/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/27/2008 10:59 AM")));
   Assert.AreEqual(nextSunday, GetSunday(DateTime.Parse("10/27/2008 11:00 AM")));
}

private DateTime GetSunday(DateTime date)
{
   if (date.DayOfWeek == DayOfWeek.Monday && date.Hour < 11)
      return date.Date.AddDays(-1);

   while (date.DayOfWeek != DayOfWeek.Sunday)
      date = date.AddDays(1);

   return date.Date;
}
Rob
A: 

I've used these extensions with great success:

http://www.codeplex.com/DateTimeExtensions

+1  A: 
DateTime GetMidnightFollowingSunday()
{
    DateTime now = DateTime.Now;
    return now.AddDays(7 - (int)now.DayOfWeek).Date;
}

If you need to start the new week after 11AM on Monday morning just subtract 11 hours from now but then it probably makes sense to name the method something else.

DateTime GetRecordDate()
{
    DateTime nowMinusOffset = DateTime.Now.AddHours(-11);
    return nowMinusOffset.AddDays(7-(int)nowMinusOffset.DayOfWeek).Date;
}
Dan Finucane
Close, but you were turning the current Sunday into the following Sunday.
MusiGenesis