Calculate WorkDays, Get Number of Days in Month in ASP
If you are looking for code to calculate workdays, then you have come to the right place. I found some examples by other people, but they never worked correctly. These examples are written in ASP.
The parameters are two dates and a year. The year is only used for the holiday function. This function will return the number of working days between those two dates. The assumption is that the dates are valid, and that the end date is greater than the start date. This function will subtract the holidays and requires the holiday function below.
WorkDays Function
' WorkDays
' returns the number of working days between two dates
'Assume that enddate is greater then startdate
'Will subtract holidays
'year is only for the holiday schedule
Public Function WorkDays(d1, d2,year)
Dim errorflag,errorflag2
Dim newDate,count,subtract,absdays
subtract=0
if not (isdate(d1) and isdate(d2)) then
response.write "At least one date is invalid."
exit function
else
d1 = cdate(d1): d2 = cdate(d2)
if d1 > d2 then
Response.Write d1&"<br>"
Response.Write d2&"<br>"
response.write "Invalid date range."
exit function
end if
end if
absdays = datediff("d", d1, d2)+1
WorkDays=absdays
count=0
do while not count=absdays
newDate=dateadd("d",count,d1)
select case datepart("w", newDate)
case 1
subtract = subtract+1
case 7
subtract = subtract+1
end select
count=count+1
loop
WorkDays=WorkDays-subtract
if WorkDays < 0 then WorkDays = 0
WorkDays=WorkDays-HolidaySchedule(d1,d2,year)
End Function
This method will return the number of holidays between two dates in a year. The parameters are the two dates and the year. Holidays sometimes fall on different days but this function assumes all the holidays fall on the same day. You might need to modify this function based on your own requirements.
Holiday Function
'Returns the number of holidays within these two dates
'This Function assumes that all the Holidays are on the same day
public Function HolidaySchedule(StartDate1,EndDate1,year1)
Dim StartDate
StartDate=cdate(StartDate1)
Dim EndDate
EndDate=cdate(EndDate1)
Dim TotalDays
Dim Holidays
Dim checkDate
Holidays=0
dim loopcount
dim newyear
loopcount=cint(year(EndDate))-cint(year(StartDate))
loopcount=loopcount+1
newyear=year(StartDate)-1
do while not loopcount=0
newyear=newyear+1
'New Years Day
checkDate=cdate("1/1/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'President's Day
checkDate=cdate("2/18/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Memorial Day
checkDate=cdate("5/26/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Independence Day
checkDate=cdate("7/4/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Labor Day
checkDate=cdate("9/1/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Thanksgiving
checkDate=cdate("11/27/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'After Thanksgiving
checkDate=cdate("11/28/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Christmas
checkDate=cdate("12/25/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Martin Luther King
checkDate=cdate("1/21/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Lincoln's BD
checkDate=cdate("2/11/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Cesar Chavez
checkDate=cdate("3/31/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Columbus Day
checkDate=cdate("10/13/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
'Veterans Day
checkDate=cdate("11/11/"&newyear)
if EndDate=>checkDate and StartDate<=checkDate then
Holidays=Holidays+1
end if
loopcount=loopcount-1
loop
HolidaySchedule=Holidays
End Function
This function will return the number of days in a month. The parameters is the month and year and the function will give you the number of total days in the month.
Get the Number of Days within a Month Function
function getDaysInMonth(strMonth,strYear)
dim strDays
Select Case cint(strMonth)
Case 1,3,5,7,8,10,12:
strDays = 31
Case 4,6,9,11:
strDays = 30
Case 2:
if ((cint(strYear) mod 4 = 0 and _
cint(strYear) mod 100 <> 0) _
or ( cint(strYear) mod 400 = 0) ) then
strDays = 29
else
strDays = 28
end if
End Select
getDaysInMonth = strDays
end function

0 comments:
Post a Comment