Results 1 to 8 of 8

Thread: Date range

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318

    Date range

    Hi all,

    Is there a way to get the dates of all weeks of a month ranging from Saturday to Wednesday?

    For example:

    The first working day of Feb is Sunday. So the label displays as follows:

    Label1(0)--> 01-04

    Label1(1)--> 07-11

    Label1(2)--> 14-18

    Label1(3)--> 21-25

    Label1(4)--> 28-29


    Please help.

    Thanks

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    This is what I have done. But it is not giving me the accurate range for the last week.

    Dim lDay As Long
    Dim lMonth As Long
    Dim dtDate As Date
    Dim Myint As Integer

    Dim T1, T2
    Myint = 0
    dtDate = DTP.Value
    lMonth = DatePart("m", dtDate)
    lDay = DatePart("d", dtDate)
    dtDate = dtDate - lDay + 1
    T1 = DatePart("d", dtDate)
    dtDate = dtDate + (4 - Weekday(dtDate))
    T2 = DatePart("d", dtDate)

    Do While lMonth = DatePart("m", dtDate)
    Label1(Myint).Caption = T1 & "-" & Day(Format(dtDate, "Short Date"))
    MsgBox T1 & "-" & Day(Format(dtDate, "Short Date"))
    T1 = DatePart("d", (dtDate + 3))
    dtDate = dtDate + 7
    If DatePart("m", dtDate) = lMonth + 1 Then
    Exit Do
    End If
    T2 = DatePart("d", dtDate)
    Myint = Myint + 1
    Load Label1(Myint)
    Label1(Myint).Move 0, Label1(Myint - 1).Top + Label1(Myint - 1).Height + 10
    Label1(Myint).Visible = True
    Loop

    Thanks.

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    VB Code:
    1. Option Explicit
    2.  
    3. Private Function GetWeeksInMonth(TheMonth As Integer, Optional TheYear As Integer = -1)
    4. Dim intDateIterate As Integer
    5. Dim WeekSatDate As Date
    6. Dim WeekWedDate As Date
    7. Dim WeekLastStart As Date
    8. Dim booAddToList As Boolean
    9.  
    10.    If TheYear < 0 Then TheYear = Year(Now)
    11.    
    12.    For intDateIterate = 1 To 36 Step 7  '1, 8, 15, 22, 29, 36
    13.       WeekLastStart = DateSerial(TheYear, TheMonth, intDateIterate)
    14.       WeekSatDate = StartDate(WeekLastStart, vbSaturday)
    15.       If StartDate(WeekLastStart, vbWednesday) < WeekSatDate Then
    16.          'Wednesday date before saturday date, adjust
    17.          WeekWedDate = StartDate(DateAdd("d", 7, WeekLastStart), vbWednesday)
    18.       Else
    19.          WeekWedDate = StartDate(WeekLastStart, vbWednesday)
    20.       End If
    21.      
    22.       booAddToList = False
    23.       'Adjust ranges
    24.       If (WeekSatDate < DateSerial(TheYear, TheMonth, 1) _
    25.          And WeekWedDate < DateSerial(TheYear, TheMonth, 1)) _
    26.       Or (WeekSatDate > DateSerial(TheYear, TheMonth + 1, 1 - 1) _
    27.          And WeekWedDate > DateSerial(TheYear, TheMonth + 1, 1 - 1)) Then
    28.          'start and end before month
    29.          'or start end after month, ignore start-end
    30.       ElseIf WeekSatDate < DateSerial(TheYear, TheMonth, 1) _
    31.       And WeekWedDate > DateSerial(TheYear, TheMonth, 1) Then
    32.          'Adjust Start to first day of month, since first day is not wednesday
    33.          WeekSatDate = DateSerial(TheYear, TheMonth, 1)
    34.          booAddToList = True
    35.       ElseIf WeekSatDate < DateSerial(TheYear, TheMonth + 1, 1 - 1) _
    36.       And WeekWedDate > DateSerial(TheYear, TheMonth + 1, 1 - 1) Then
    37.          'Adjust wed to last day of month, since last day is not a sat
    38.          WeekWedDate = DateSerial(TheYear, TheMonth + 1, 1 - 1)
    39.          booAddToList = True
    40.       Else  'within Month
    41.          booAddToList = True
    42.       End If
    43.      
    44.       If booAddToList = True Then
    45.          List1.AddItem Format(WeekSatDate, "dd") & " - " & Format(WeekWedDate, "dd")
    46.       End If
    47.    Next
    48. End Function
    49.  
    50. Private Function StartDate(InputDate As Date, WeekFirstDay As Integer) As Date
    51.    'Function I use to get the date of the first day of the week
    52.    'where InputDate belongs to.
    53.    StartDate = DateSerial(Year(InputDate), Month(InputDate), (Day(InputDate) - Weekday(InputDate, WeekFirstDay) + 1))
    54. End Function
    55.  
    56. Private Sub Command1_Click()
    57.    Call GetWeeksInMonth(Month(Now))
    58. End Sub
    Last edited by leinad31; Feb 22nd, 2004 at 05:12 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    Thank you so much. Exactly what I wanted. But I noticed that, if the last day of the month is a Saturday, it counts to the 4th of the next month. Can that error be rectified?

    Thanks

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    I did that before I had to rush to a dinner date so I didn't check it

    VB Code:
    1. Option Explicit
    2.  
    3. Private Function GetWeeksInMonth(TheMonth As Integer, Optional TheYear As Integer = -1)
    4. Dim intDateIterate As Integer
    5. Dim WeekSatDate As Date
    6. Dim WeekWedDate As Date
    7. Dim WeekLastStart As Date
    8. Dim booAddToList As Boolean
    9.  
    10.    If TheYear < 0 Then TheYear = Year(Now)
    11.    
    12.    For intDateIterate = 1 To 36 Step 7  '1, 8, 15, 22, 29, 36
    13.       WeekLastStart = DateSerial(TheYear, TheMonth, intDateIterate)
    14.       WeekSatDate = StartDate(WeekLastStart, vbSaturday)
    15.       If StartDate(WeekLastStart, vbWednesday) < WeekSatDate Then
    16.          'Wednesday date before saturday date, get wednesday in next week
    17.          WeekWedDate = StartDate(DateAdd("d", 7, WeekLastStart), vbWednesday)
    18.       Else
    19.          WeekWedDate = StartDate(WeekLastStart, vbWednesday)
    20.       End If
    21.      
    22.       booAddToList = False
    23.       If (Month(WeekSatDate) = TheMonth) And (Month(WeekWedDate) = TheMonth) Then
    24.          'Both within month
    25.          booAddToList = True
    26.       ElseIf Month(WeekSatDate) < TheMonth _
    27.       And WeekWedDate > DateSerial(TheYear, TheMonth, 1) Then
    28.          'Sat not within month
    29.          'Adjust Start to first day of month, since first day is not wednesday
    30.          WeekSatDate = DateSerial(TheYear, TheMonth, 1)
    31.          booAddToList = True
    32.       ElseIf (Month(WeekWedDate) > TheMonth) _
    33.       And WeekSatDate < DateSerial(TheYear, TheMonth + 1, 1 - 1) Then
    34.          'Wed not within month
    35.          'Adjust wed to last day of month, since 2nd to the last day is not a sat
    36.          WeekWedDate = DateSerial(TheYear, TheMonth + 1, 1 - 1)
    37.          booAddToList = True
    38.       'Else
    39.          'Both not in month, ignore
    40.       End If
    41.      
    42.       If booAddToList = True Then
    43.          List1.AddItem Format(WeekSatDate, "dd") & " - " & Format(WeekWedDate, "dd")
    44.       End If
    45.    Next
    46. End Function
    47.  
    48. Private Function StartDate(InputDate As Date, WeekFirstDay As Integer) As Date
    49.    StartDate = DateSerial(Year(InputDate), Month(InputDate), (Day(InputDate) - Weekday(InputDate, WeekFirstDay) + 1))
    50.    'Or StartDate = DateAdd("d", 1 - WeekDay(InputDate, WeekFirstDay), InputDate)
    51. End Function
    52.  
    53. Private Sub Command1_Click()
    54.    Call GetWeeksInMonth(Month(Now))
    55. End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    This does not display the last week at all.

    Take for example, July. July has only one working day in the last week, that is, Saturday. I such conditions, it is better that the last label displays like 31-31. Same is the case with September too because, in the first week of September, there is only one working day, that is, 1. So the first label will better display 1-1.

    Thanks

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    VB Code:
    1. Option Explicit
    2.  
    3. Private Function GetWeeksInMonth(TheMonth As Integer, Optional TheYear As Integer = -1)
    4. Dim intDateIterate As Integer
    5. Dim WeekSatDate As Date
    6. Dim WeekWedDate As Date
    7. Dim WeekLastStart As Date
    8. Dim booAddToList As Boolean
    9.  
    10.    If TheYear < 0 Then TheYear = Year(Now)
    11.    
    12.    For intDateIterate = 1 To 36 Step 7  '1, 8, 15, 22, 29, 36
    13.       WeekLastStart = DateSerial(TheYear, TheMonth, intDateIterate)
    14.       WeekSatDate = StartDate(WeekLastStart, vbSaturday)
    15.       If StartDate(WeekLastStart, vbWednesday) < WeekSatDate Then
    16.          'Wednesday date before saturday date, get wednesday in next week
    17.          WeekWedDate = StartDate(DateAdd("d", 7, WeekLastStart), vbWednesday)
    18.       Else
    19.          WeekWedDate = StartDate(WeekLastStart, vbWednesday)
    20.       End If
    21.      
    22.       booAddToList = False
    23.       If (Month(WeekSatDate) = TheMonth) And (Month(WeekWedDate) = TheMonth) Then
    24.          'Both within month
    25.          booAddToList = True
    26.       ElseIf (Month(WeekSatDate) < TheMonth) And (Month(WeekWedDate) = TheMonth) Then
    27.          'Sat not within month
    28.          'Adjust Start to first day of month
    29.          WeekSatDate = DateSerial(TheYear, TheMonth, 1)
    30.          booAddToList = True
    31.       ElseIf (Month(WeekSatDate) = TheMonth) And (Month(WeekWedDate) > TheMonth) Then
    32.          'Wed not within month
    33.          'Adjust wed to last day of month
    34.          WeekWedDate = DateSerial(TheYear, TheMonth + 1, 1 - 1)
    35.          booAddToList = True
    36.       'Else
    37.          'Both not in month, ignore
    38.       End If
    39.      
    40.       If booAddToList = True Then
    41.          List1.AddItem Format(WeekSatDate, "dd") & " - " & Format(WeekWedDate, "dd")
    42.       End If
    43.    Next
    44. End Function
    45.  
    46. Private Function StartDate(InputDate As Date, WeekFirstDay As Integer) As Date
    47.    StartDate = DateSerial(Year(InputDate), Month(InputDate), (Day(InputDate) - Weekday(InputDate, WeekFirstDay) + 1))
    48.    'Or StartDate = DateAdd("d", 1 - WeekDay(InputDate, WeekFirstDay), InputDate)
    49. End Function
    50.  
    51. Private Sub Command1_Click()
    52.    Call GetWeeksInMonth(Month(Now))
    53. End Sub
    Last edited by leinad31; Feb 23rd, 2004 at 04:29 AM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318
    Thanks. That was excellent! Thanks once again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width