vb6c1flexgrid

How to create Header "Date Range per Week" in Flexgrid


Help me to be able to suss out the problem on Visual Basic coding using the "C1 FlexGrid".

How to get a date range that would show the number of week as well?

For example:

   January 2003
 S  M Tu  W Th  F  S
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

I would like to show

01/01/03 - 04/01/03 as "week 1" of January 03
01/05/03 - 01/11/03 as "week 2" of January 03
01/12/03 - 18/01/03 as "week 3" in January 03 ... up "week 5", etc. ..

Is there a way I can do this?


Solution

  • Not tested. Improvement possible. Make adjustments to code as per your requirement.

    Public Function DateOfFirstDayofWeek(intCurrentDayofWeek As Integer, WhichDate As Date) As Date
        DateOfFirstDayofWeek = DateAdd("D", intCurrentDayofWeek * (-1) + 2, WhichDate)
    End Function
    
    Function dhFirstDayInMonth(dtmDate As Date) As Date
        dhFirstDayInMonth = DateSerial(year(dtmDate), Month(dtmDate), 1)
    End Function
    
    Function dhLastDayInMonth(dtmDate As Date) As Date
        dhLastDayInMonth = DateSerial(year(dtmDate), Month(dtmDate) + 1, 0)
    End Function
    
    Private Sub getWeekRange_Click()
    Dim startDate As Date
    Dim endDate As Date
    Dim tmpDate  As Date
    Dim dateOfMonth As Date
    Dim myDate As Date
    
    dateOfMonth = CDate("01/02/2013") 
    endDate = dhLastDayInMonth(dateOfMonth)
    startDate = dhFirstDayInMonth(dateOfMonth)
    tmpDate = startDate
    
    While tmpDate < endDate
        myDate = DateOfFirstDayofWeek(Weekday(tmpDate, vbSunday), tmpDate) ''Assuming Week starts with Sunday
        tmpDate = DateAdd("d", 6, myDate)
        If myDate < startDate Then
            myDate = startDate
        ElseIf tmpDate > endDate Then
            tmpDate = endDate
        End If
        Debug.Print myDate & "      " & tmpDate 'This Prints the Start date and End date of every week
    Wend
    End Sub