# Thursday, April 22, 2010
« Verify users against Active Directory us... | Main | Hide and go seek. Hiding and showing ele... »

I recently needed to make a change to some code that had a routine for getting the week currently in.  I needed to add the ability to get the previous / last week relative to this week.  No problem, I would just work off of the existing routine.

Private Sub SetDatesToThisWeek()

  Dim dateStart As String
  Dim dateEnd As String

  Dim dateNow As Date
  dateNow = Now

  dateStart = (Weekday(dateNow, Microsoft.VisualBasic.FirstDayOfWeek.Sunday) - 1).ToString
  dateStart = DateAdd(DateInterval.Day, -CType(dateStart, Double), dateNow).ToString
  dateStart = Format(System.Convert.ToDateTime(dateStart), "MM/dd/yyyy")

  dateEnd = DateAdd(DateInterval.Day, 6, System.Convert.ToDateTime(dateStart)).ToString
  dateEnd = Format(System.Convert.ToDateTime(dateEnd), "MM/dd/yyyy")
 
  Me.StartDate.Text = dateStart
  Me.EndDate.Text = dateEnd
End Sub

It took me a little bit of time to digest the routine and realized I needed to simplify the routine and even though the code is in VB I wanted to remove the dependence on the Microsoft.VisualBasic library as well.  This means getting rid of Weekday, DateAdd, etc and replace with the framework standard.  I love not reinventing the wheel and happened to find a nice pointer on stack overflow.

Private Sub SetDatesToThisWeek()

  Dim thisSunday As Date = DateTime.Now.AddDays(-(DateTime.Now.DayOfWeek - DayOfWeek.Sunday))
  Me.StartDate.Text = Format(thisSunday, "MM/dd/yyyy")
  Me.EndDate.Text = Format(thisSunday.AddDays(6), "MM/dd/yyyy")

End Sub

11 lines of code and three variables down to three lines and one variable with the added benefit of being easier to read, understand and the reliance on Microsoft.VisualBasic gone (at least for this code). Since I needed to create another routine that returns last week I decided as suggested in one of the stack overflow answers to create an extension on the date type.

Imports System.Runtime.CompilerServices

Module DateExtensions

  <Extension()> _
  Public Function StartOfWeek(ByVal aDate As Date, ByVal dayToStartWeek As DayOfWeek) As Date
    Return aDate.AddDays(-(aDate.DayOfWeek - dayToStartWeek))
  End Function
End Module

Now I can use the extension like so:

Private Sub SetDatesToLastWeek()

  Dim lastSunday As Date = DateTime.Now.StartOfWeek(DayOfWeek.Sunday).AddDays(-7)
  Me.StartDate.Text = Format(lastSunday, "MM/dd/yyyy")
  Me.EndDate.Text = Format(lastSunday.AddDays(6), "MM/dd/yyyy")
End Sub

Private Sub SetDatesToThisWeek()

  Dim thisSunday As Date = DateTime.Now.StartOfWeek(DayOfWeek.Sunday)
  Me.StartDate.Text = Format(thisSunday, "MM/dd/yyyy")
  Me.EndDate.Text = Format(thisSunday.AddDays(6), "MM/dd/yyyy")
End Sub
The C# version of the extension is a little different than VB:
namespace Extensions
{
    public static class DateExtension
    {
        public static Date StartOfWeek(this Date aDate, DayOfWeek dayToStartWeek)
        {
            return aDate.AddDays(-(aDate.DayOfWeek - dayToStartWeek));
        }
    }   
}

Leaving code better off than when you started is always a great feeling.

Happy Coding!

P.S. – I also replaced the date formatting strings with a constant. :-)

C# | Programming | VB
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview