Wrox Programmer Forums
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 13th, 2005, 10:21 AM
Registered User
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default Date difference.

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 = 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 (d = 1 or d = 7) then
                        cntr1 = cntr1 + 1
                    end if
            wday = iFD - cntr1

            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

            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.

Old July 13th, 2005, 05:48 PM
Friend of Wrox
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41

Please do not cross post:

Wind is your friend
Old July 14th, 2005, 04:11 AM
Registered User
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen

Okay will take care of this.

Old July 14th, 2005, 05:23 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Does 16-07 and 17-07 return the same results as 15-07?

Maybe you're using a wrong date format and your date is silently converted.

For example 12-7 *could* be december 7, depending on your regional settings. However, 15-07 cannot mean anything else than july 15th.

The conversion can be done by the database if you're not aware of it. Try to use the ISO format (YYYYMMDD) and see if that makes a difference....

Imar Spaanjaars
Everyone is unique, except for me.
Old July 18th, 2005, 05:11 AM
Registered User
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen

No, 16-07 and 17-07 returns 1.
If I be using wrong date format, it should not work for other dates, i.e.14-07 and 16-07,etc.

Can you work out with this function and check at your end so that you can find out what is wrong exctly.

Thanks in advance.

Old July 19th, 2005, 06:06 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post

Hi Rupen,

Not just for 15th, looks like it does behave that way for any fridays.

When you mention 14-07 and 15-07 there, what is the result expected? If that is to be 2, I am afraid if that works, coz, I see 1 for that.

I think the catch is here.
if (d = 1 or d = 7) then
Looks like the week starts there with saturday as 1, thus sunday is 2, not 7
if (d = 1 or d = 2) then
This did the trick. I got to stick to this, as 13-07 was showing 5 as value of d within the FOR loop, when i=0.

BTW, I had to comment the DB related lines, as I don't have any idea what it contains in the holidaymaster table. I assume you have the holidays other than saturday sundays in it. So that shouldn't be a problem for the dates I used.

Hope that helps.

- Vijay G
Strive for Perfection
Old July 21st, 2005, 06:48 AM
Registered User
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen

Hi Vijay,

You are right it is not counting any Friday. But I am amazed, why so?

For 14-07 to 15-07 it should return 2, and not 1.

For if (d = 1 or d = 7) then
1 refers to Sunday and 7 is to Saturday,

Pls refer this:

Now, the catch is here:

          for i = 0 to iFD

It should be:

          for i = 0 to iFD - 1

This will work fine!!!

Pleas pass your view on this.

Rupen Anjaria.
We CAN'T avoid problems, but of course can solve it.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date difference surya Javascript How-To 2 September 19th, 2008 12:42 AM
get date difference benz_jie2005 Access 1 January 16th, 2008 09:33 AM
Date Difference Help saf01 Crystal Reports 0 September 29th, 2005 05:35 AM
Difference between Date and Now() marcin2k Access VBA 3 September 8th, 2005 01:05 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.