我如何计算到确定的日期还有多少个工作日?

How can I calculate how many working days are to a determined date?

我需要制作类似工作日计数器的东西,但我不知道怎么做:

假设有人在今天 04-25-2019 提出请求,需要在 15 个工作日内获得批准,也就是 05-16-2019 左右,这怎么办?我还不知道,我应该包括我的国家的假期。

这将是:

01-01-2019
19-04-2019
20-04-2019
01-05-2019
21-05-2019
29-06-2019
16-07-2019
15-08-2019
18-09-2019
19-09-2019
20-09-2019
12-10-2019
31-10-2019
01-11-2019
08-12-2019
25-12-2019

我试过这个:

<%
DateFrom = "10/1/2012"
DateTo = "10/31/2012"
Weekends = 0
ActualDays = 0

' Step 1: Get the actual days
ActualDays = DateDiff("d", DateFrom, DateTo)

' Step 2: Find the weekends
For x = 0 To ActualDays - 1
    xDate = DateAdd("d", x, DateFrom)
    If Weekday(xDate, 1) = 1 Or Weekday(xDate, 1) = 7 Then
        Weekends = Weekends + 1
    End If
Next

WorkingDays = ActualDays - Weekends

response.Write "ActualDays: "&ActualDays  &" Weekends: "& Weekends &" WorkingDays: "& WorkingDays
%>

但结果并不如我所愿。

您的方向是对的,您只需要同时检查国定假日(如果不是周末),然后从实际天数中减去周末和国定假日即可得到工作日数。试试这个:

' You need to keep a record of national holidays and keep it updated. Probably best to do this
' outside of your function. You can't use a constant to store an array, you could just dim a 
' regular variable, but IMO a better alternative would be to use "Application" and set it in  
' your global.asa file under "Sub Application_OnStart"... or just use a database.

Application("NationalHolidays") = array("01-01-2019","19-04-2019","20-04-2019","01-05-2019","21-05-2019","29-06-2019","16-07-2019","15-08-2019","18-09-2019","19-09-2019","20-09-2019","12-10-2019","31-10-2019","01-11-2019","08-12-2019","25-12-2019")


function calcDays(fromDate,toDate)

    Dim ActualDays, Weekends, NationalHolidays, WorkingDays, nhIndex, x, y

    fromDate = cDate(fromDate)
    toDate = cDate(toDate)

    ActualDays = DateDiff("d",fromDate,toDate)      
    Weekends = 0    
    NationalHolidays = 0
    WorkingDays = 0
    nhIndex = 0

    ' Loop from start day to end day

    for x = 0 to ActualDays + 1

        ' Count the weekends

        if WeekDay(DateAdd("d",x,fromDate)) = 1 OR WeekDay(DateAdd("d",x,fromDate)) = 7 then
            Weekends = Weekends + 1
        else

            ' Check for national holidays if it's not a weekend

            if NOT nhIndex > uBound(Application("NationalHolidays")) then
                for y = nhIndex to uBound(Application("NationalHolidays"))
                    ' This if/else logic assumes Application("NationalHolidays") is in ascending order.
                    if DateAdd("d",x,fromDate) = cDate(Application("NationalHolidays")(y)) then
                        NationalHolidays = NationalHolidays + 1
                        ' Keep track of the last national holiday found and start from that 
                        ' position + 1 on the next check.
                        nhIndex = y + 1
                        exit for
                    elseif cDate(Application("NationalHolidays")(y)) < DateAdd("d",x,fromDate) then
                        ' Keep count of national holidays that have already passed and skip them
                        ' on the next check.
                        nhIndex = y + 1
                    elseif cDate(Application("NationalHolidays")(y)) > DateAdd("d",x,fromDate) then
                        ' The national holiday dates have exceeded the current date, there's no
                        ' point in continuing checking.
                        exit for
                    end if
                next
            end if

        end if

    next

    ' Working days is the number of days between "fromDate" and "toDate" minus weekends and national holidays

    WorkingDays = (ActualDays-Weekends-NationalHolidays)

    calcDays = "<p>Actual Days: " & ActualDays & "</p>" &_
    "<p>National Holidays: " & NationalHolidays & "</p>" &_
    "<p>Weekends: " & Weekends & "</p>" &_
    "<p>Working Days: " & WorkingDays & "</p>"

end function

response.write calcDays("29-04-2019","13-05-2019")

April 29th 2019 to May 13th 2019

输出:

Actual Days: 14
National Holidays: 1
Weekends: 4
Working Days: 9


编辑: 要计算 Date X + Y Working Days = Date Z (如您的问题所述),您可以使用这个功能:

function AddWorkingDays(startDate,workingDays)

    startDate = cDate(startDate)

    Dim DayCount, workingDayCount, nhIndex, isNationalHoliday, x

    DayCount = 0
    workingDayCount = 0
    nhIndex = 0

    do until workingDayCount = workingDays

        ' Make sure it's not a weekend

        if NOT(WeekDay(DateAdd("d",DayCount,startDate)) = 1 OR WeekDay(DateAdd("d",DayCount,startDate)) = 7) then

            isNationalHoliday = false ' default

            ' Make sure it's not a national holiday

            if NOT nhIndex > uBound(Application("NationalHolidays")) then
                for x = nhIndex to uBound(Application("NationalHolidays"))
                    ' This if/else logic assumes Application("NationalHolidays") is in ascending order.
                    if DateAdd("d",DayCount,startDate) = cDate(Application("NationalHolidays")(x)) then
                        isNationalHoliday = true
                        ' Keep track of the last national holiday found and start from that 
                        ' position + 1 on the next check.
                        nhIndex = x + 1
                        exit for
                    elseif cDate(Application("NationalHolidays")(x)) < DateAdd("d",DayCount,startDate) then
                        ' Keep count of national holidays that have already passed and skip them
                        ' on the next check.
                        nhIndex = x + 1
                    elseif cDate(Application("NationalHolidays")(x)) > DateAdd("d",DayCount,startDate) then
                        ' The national holiday dates have exceeded the current date, there's no
                        ' point in continuing checking.
                        exit for
                    end if
                next
            end if

            if NOT isNationalHoliday then workingDayCount = workingDayCount + 1 ' It's a working day!

        end if

        ' Keep count of the total number of days needed to make up the working day target

        DayCount = DayCount + 1

    loop

    AddWorkingDays = DateAdd("d",DayCount,startDate)

end function

' As in your question, calculate 15 working days from April 25th 2019
response.write AddWorkingDays("25-04-2019",15)

April 25th 2019 + 15 Working Days

输出:

May 17th 2019