UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> WeekDays between dates - alternate approach #2    
 
   
r_cubed
post Nov 12 2003, 10:52 PM
Post #1

UtterAccess VIP
Posts: 3,752
From: Australia (NSW)



My first "WeekDays" post into Archives used the ByRef option to the function (requiring data variables to be defined in the "calling" code prior to running the function (thus enabling multiple values to be returned from the function, i.e. ALL of the weekdays individually counted and returned individually)), which could be useful in some circumstances. (I have that circumstance).

However, it may be that you (the developer) only wants to find out "how many Mondays ONLY" occur between dates, so the use and requirement to define variable prior to the "function call" could be 'over-kill'.

The following function can be used to return ONE value containing the "count of a SPECIFIC nominated 'Day of the Week' ONLY". The specified day required is passed via the 3rd parameter to the function (refer function comments for valid values to be used).

' ------------------------------------------------------------------------------------------------------

CODE
Function DayNames_Between_Dates(inDate1 As Date, _
                                inDate2 As Date, _
                                strReqdDay As Variant) _
                           As Long
                                      
    ' Return the number of times a SPECIFIC "Day of Week" occurs between the dates input
    ' i.e. How many Mondays, or Tuesdays, etc etc ....based on the 3rd parameter passed
    '
    ' Inputs:   inDate1     First Date
    '           inDate2     Second Date (in Range)
    '           strReqdDay  any ONE of:     1 or  "Sun", "Sunday", _
    '                                       2 or  "Mon", "Monday", _
    '                                       3 or  "Tue", "Tues", "Tuesday", _
    '                                       4 or  "Wed", "Wednesday", _
    '                                       5 or  "Thu", "Thur", "Thurs", "Thursday", _
    '                                       6 or  "Fri", "Friday", _
    '                                       7 or  "Sat", "Saturday"
                                          
    Dim DaysCount As Integer
    Dim DayAdjuster As Integer
    Dim dteIn1 As Date
    Dim dteIn2 As Date
    
    ' Set input dates in correct date sequence
    
    If inDate1 <= inDate2 Then
        dteIn1 = inDate1
        dteIn2 = inDate2
    Else
        dteIn1 = inDate2
        dteIn2 = inDate1
    End If
        
    If Not IsNumeric(strReqdDay) Then
        ' If parameter passed is 'Text', then convert it into the equivalent numeric for use later
        Select Case strReqdDay
            Case "Sun", "Sunday"
                strReqdDay = 1
            Case "Mon", "Monday"
                strReqdDay = 2
            Case "Tue", "Tues", "Tuesday"
                strReqdDay = 3
            Case "Wed", "Wednesday"
                strReqdDay = 4
            Case "Thu", "Thur", "Thurs", "Thursday"
                strReqdDay = 5
            Case "Fri", "Friday"
                strReqdDay = 6
            Case "Sat", "Saturday"
                strReqdDay = 7
         End Select
    End If
    
    DayAdjuster = 1
        
    Do Until dteIn1 > dteIn2
        
        If DatePart("w", dteIn1, vbSunday) = strReqdDay Then       ' Only work with REQUIRED day
            DaysCount = DaysCount + 1
            ' Once the first reqd 'Day' is found, the function can
            ' THEN jump forward a week at a time (quicker processing)
            DayAdjuster = 7
        End If
        
        dteIn1 = DateAdd("d", DayAdjuster, dteIn1)
    Loop
  
   DayNames_Between_Dates = DaysCount
  
End Function
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th May 2013 - 04:59 PM