How do I calculate the last business day of month in VBScript? It is for a Reporting Services report.
Thanks
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)
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.
Here is one solution for anyone who is looking to get Last business day of the previous month.
Dim lastbusinessdayofprevmonth
Sub GetLastDay()
Dim curdate
curdate = Date()
Dim firstdayofcurmonth
firstdayofcurmonth = Month(curdate) & "/1/" & Year(curdate)
Dim lastdayofprevmonth
lastdayofprevmonth = DateAdd("d", -1, firstdayofcurmonth)
Dim day
day = weekday(lastdayofprevmonth)
if(day = 1) then
lastbusinessdayofprevmonth = DateAdd("d", -2, lastdayofprevmonth)
elseif (day = 7) then
lastbusinessdayofprevmonth = DateAdd("d", -1, lastdayofprevmonth)
else
lastbusinessdayofprevmonth = lastdayofprevmonth
end if
end sub
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
来源:https://stackoverflow.com/questions/302597/how-to-calculate-last-business-day-of-month-in-vbscript