KnowDotNet Visual Organizer

Finding Start and End Dates of Previous Week

Using DateAdd With Offsets

by Les Smith
Print this Article Discuss in Forums

Did you ever need to know what the date of the first day of the previous week was, regardless of where you are in the current week?  Well, I need to know the date of Monday and last Sunday of the previous week.  And I may need to know that on any day of the current week.

Sounds like a wild request doesn't it?  But, I need to run a report from a SQL Server Query and I could run the report on any day of the current week, but only want to see results based on last week's Monday and the following Sunday.  In other words, I want to run a report for jobs completed on the previous Monday through Sunday and I don't know which day of the current week that I may need to run the report.

Maybe you may have such a seemingly trivial and yet sosphisticated request.  This little set of code will show you how to do that in VB.NET.  Obviously, you coud do the same thing in C# but since C# does not have a DateAdd funciton, the code will be a little different.

The GetDates Sub, shown below, returns the date of the previous Monday in stDate and the following Sunday in endDate.  Both variables are passed to a Sub because a Function can only return one value.  Obviously, the function could have returned the Monday date and then the caller could have added 6 days to that date, but I wanted the GetDates method to do all of the work.

The algorithm is simple.  Since I do not know what day of the current week that I wll be running the report, I first determine which day of the week that we are currently in and produce an offset denoting that many days from Monday.  If we are running on Monday, then the offset is zero, or if on Tuesday, then 1, etc.

Once the offset is determined, the DateAdd function does the rest of the work.

''' <summary>
''' this method returns the previous Monday (last week) in stDate
''' and today (if Monday) else the Monday of this week.
''' </summary>
''' <param name="stDate" ></param>
''' <param name="endDate" ></param>
''' <remarks></remarks>
Private Sub GetDates(ByRef stDate As Date, ByRef endDate As Date)
   Dim offset As Double = 0
   Select Case Today.DayOfWeek
      Case DayOfWeek.Monday : offset = 0
      Case DayOfWeek.Tuesday : offset = -1
      Case DayOfWeek.Wednesday : offset = -2
      Case DayOfWeek.Thursday : offset = -3
      Case DayOfWeek.Friday : offset = -4
      Case DayOfWeek.Saturday : offset = -5
      Case DayOfWeek.Sunday : offset = -6
   End Select
   endDate = DateAdd(DateInterval.Day, offset, Today)
   stDate = DateAdd(DateInterval.Day, -7 + offset, Today)
End Sub

If you are developing in C#, you would use the following code.  I could have done it in VB.NET the same way, but I developed it in VB.NET first and then converted it to C#.  Since I have been doing VB much longer than C#, when in VB.NET I still use some of the old classic VB functions.  Sorry about that, but old habits are hard to break.

private void GetDates(ref System.DateTime stDate, ref System.DateTime endDate)
{
  
double offset = 0;
  
switch (System.DateTime.Today.DayOfWeek)
   {
      
case DayOfWeek.Monday:
      offset = 0;
      
break;
      
case DayOfWeek.Tuesday:
      offset = -1;
      
break;
      
case DayOfWeek.Wednesday:
      offset = -2;
      
break;
      
case DayOfWeek.Thursday:
      offset = -3;
      
break;
      
case DayOfWeek.Friday:
      offset = -4;
      
break;
      
case DayOfWeek.Saturday:
      offset = -5;
      
break;
      
case DayOfWeek.Sunday:
      offset = -6;
      
break;
   }
   endDate = System.DateTime.Today.AddDays(offset);
   stDate = System.DateTime.Today.AddDays(-7 + offset);
}


If you have any comments or suggestions on this article or any other programming subject you would like to discuss, comment on my blog at Click Here.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing