如何计算VBScript中的最后一个工作日

时间:2022-10-16 10:01:05

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

如何计算VBScript中的最后一个工作日?它用于Reporting Services报告。

Thanks

4 个解决方案

#1


6  

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)

#2


2  

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

这里有一篇很好的CodeProject文章:与商业日期(商务假期/周末等)合作。

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.

P.S。:你会在文章中找到函数LastDayOfMonth和isHoliday。

#3


1  

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

#4


0  

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

如果您指的是该月的最后一周(M-F),请尝试:

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

#1


6  

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)

#2


2  

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

这里有一篇很好的CodeProject文章:与商业日期(商务假期/周末等)合作。

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.

P.S。:你会在文章中找到函数LastDayOfMonth和isHoliday。

#3


1  

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

#4


0  

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

如果您指的是该月的最后一周(M-F),请尝试:

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