Getting dates for ThisWeek, LastWeek, etc.

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
Does anyone have code to share to get the inclusives dates for the "current week", "last week" etc.?
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
Here's a function that I took from a Winform calendar control I'm currently working on. It returns an arraylist of dates and accepts two parameters; the 'firstDayOfWeek' parameter specifies which day the list should begin with, the 'iWeeksAgo' parameter is an integer value which specifies the week (0 would return this week, 1 would return last week, -1 next week, ...):

VB.NET:
Public Function GetWeeksDates(ByVal firstDayOfWeek As DayOfWeek, _
  ByVal iWeeksAgo As Integer) As ArrayList
    Dim al As New ArrayList()
    Dim day As DateTime = DateTime.Now
    Dim startDay As Integer = Int(day.DayOfWeek) - Int(firstDayOfWeek)
    If startDay <= 0 Then startDay += 7
    startDay = startDay + (7 * iWeeksAgo)
    day = day.AddDays(-startDay)
    Dim i As Integer
    For i = 0 To 6
        al.Add(day.Date)
        day = day.AddDays(1)
    Next
    Return al
End Function

Hope you find it useful.
 
Top Bottom