Date difference.
Hi,
Below is a function which counts working days(i.e. don't count sunday and saturday) between specified dates.
--------------------------------------------------
Function GetWorkingDay(dtFrom1,dtTo1)
dim cd
dim iFD
dim cntr1
dim i
dim d
dim wday
dim objRSHL
set objRSHL = Server.CreateObject("ADODB.Recordset")
'IFD STORES NUMBER OF DAY IN CURRENT MONTH. SAY 30 FOR JUNE
'iFD = datepart("d",dateserial(year(cd),month(cd)+1,1-1))
iFD = DateDiff("d",dtFrom1,dtTo1) + 1
cntr1 = 0
for i = 0 to iFD
'd = datepart("w",dateadd("d",i,dateserial(year(cd),mon th(cd),1)))
d = datepart("w",dateadd("d",i,CDate(dtFrom1)))
'IF IT IS SUNDAY OR SATURDAY INCREMENT COUNTER
if (d = 1 or d = 7) then
cntr1 = cntr1 + 1
end if
next
wday = iFD - cntr1
'GET HOLIDAYS BETWEEN THE DATE SPECIFIED
objRSHL.open "select count(holidayid) from holidaymaster where HolidayType = 1 and HolidayDate between #" & CDate(dtFrom1) & "# and #" & CDate(dtTo1) & "#" ,objConn
if not objRSHL.EOF then
wday = wday - objRSHL.fields(0).value
end if
objRSHL.close
GetWorkingDay = wday
End Function
--------------------------------------------------
It works fine for almost all date range. Except 15-Jul-05, strange but don't know why.
e.g. if you pass value as 12-Jul-05 to 12-Jul-05 it will return 1
but if 15-Jun-05 to 15-Jun-05 will return 0.
Thankx in advance.
|