views:

1820

answers:

3

How do I caculate the last business day of month in VBScript. It is for a Reporting Services report.

Thanks

A: 

If you mean the last week day of the month (M-F), then try:

Dim d

d = DateAdd("m", 1, Now)

d = Month(d) & "/1/" & Year(d)
d = DateAdd("d", -1, d)

If Weekday(d) = 7 Then
    d = DateAdd("d", -1, d)
ElseIf Weekday(d) = 1 Then
    d = DateAdd("d", -2, d)
End If

MsgBox d
P Daddy
A: 

There is a good CodeProject article here: Working With Business Dates (Business Holidays/Weekends, etc).

This project is aimed at easing the troubles of figuring out valid business dates. It includes a few functions I have created to determine whether or not the date is a holiday or a weekend, and also to retrieve either the next or last business day. There is also a function to find the last day of a given month.

Function LastBusinessDay(sDate)

   Dim iDay, iDaysToAdd, iDate

   iDaysToAdd = 0
   iDate = sDate

   x = 1

   Do while iDaysToAdd >= 0

      If Weekday(iDate) = 1 or Weekday(iDate) = 7 or _
                isHoliday(iDate) <> 0 then
         iDay = Weekday(iDate)
         Select Case cint(iDay)
            Case 1  'Sunday

               iDate = DateAdd("d", -1, iDate)

            Case 7  'Saturday

               iDate = DateAdd("d", -1, iDate)

            Case else    'this is a valid day

                if isHoliday(iDate) > 0 then
                    iDate = dateadd("d", -(isHoliday(iDate)), iDate)
                else
                    iDaysToAdd = iDaysToAdd - 1
                end if

         End Select
      end if
   Loop

   LastBusinessDay = iDate
End Function

P.S.: You'll find the functions LastDayOfMonth and isHoliday in the article.

splattne
+2  A: 

How about:

intMonth=11
'Use zero to return last day of previous month '
LastDayOfMonth= dateserial(2008,intMonth+1,0)

'Saturday '
If WeekDay(LastDayOfMonth,1)=7 Then LastDayOfMonth=LastDayOfMonth-1
'Sunday '
If WeekDay(LastDayOfMonth,1)=1 Then LastDayOfMonth=LastDayOfMonth-2

Msgbox LastDayOfMonth & "  " & Weekdayname(Weekday(LastDayOfMonth,1),1)
Remou
+1 for using "first day of next month" algorithm -- delegates the calendar details to library routines.
S.Lott