Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Weekday Diff

Status
Not open for further replies.

fredpeters555

Technical User
Jul 16, 2003
19
CA
AAAAACKK" says Bill the cat!
I need to get the balance of work days left in the current month but I can't even get the number of weekdays beween two hard coded dates. It just wants to return weeks.
<%
var1 = "5/10/2004"
var2 = "5/12/2004"
Response.Write("var1 to var2 is " & DateDiff("w", var1, var2) & " workdays ")

%>

should return 2 yet returns 0 and will continue to return weeks if I adjust the variable.
TIA
Fred
 
Err..it's returning the right value. Your asking it for the difference in weeks.

You'll likely have to write a little function to count the days for you, ignoring sat + sun. maybe something like:
Code:
Function WorkDayDiff(start_date,end_date)
   'dim some temporary variables
   Dim t_date, diff_ctr
   'init counter at 0
   diff_ctr = 0
   'set the temporary date to the start date
   t_date = start_date

   'start the loop - loop until the dates match
   Do Until start_date >= end_date

      'If the weekday is mon-fri then count it
      If WeekDay(t_date) > 1 AND WeekDay(t_date) < 7 Then
         diff_ctr = diff_ctr + 1
      End If
      
      'increment the date by one day
      t_date = DateAdd("d",1,t_date)
   Loop

   'set the return value to the counter
   WorkDayDiff = diff_ctr
End Function

Thats a little longer than it has to be, but I wanted to lead you through what was going on.

Offtopic Slightly: Now, can anyone give me a 4 line or less solution to this? Counting the Function/End Function? :)

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
thanks for the quick response. I'll look closely at it

I've gone to what seemed like a gazillion ASP pages which all stated that "w" returned weekday and "ww" returned weeks.

For instance
<%
var1 = "5/01/2004"
var2 = "6/01/2004"
Response.Write("var1 to var2 is " & DateDiff("w", var1, var2) & " workdays ")

%>
returns 4

While the week parameter "ww" returns 5.

oops...I just looked my post over and it didn't state I was working in ASP. Sorry if that sent you on a wild goose chase.
Fred
 
Odd, I've never seen the w vs ww thing, then again I generally don't use dateDiff more than twce a year so I'm not sure I would remember if I had.

The ASP part I took for granted since this is the ASP forum and VBScript is VBScript, not much else looks like it ;)

I'll tak a look at that week vs weekdays thing and see if maybe I am wrong about what it should be outputting.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
ok, heres the lowdown on what realy happens.

I found several sites that also said it either wrong or badly. What is happening is that using "w" in DateDiff figures out the number of weeks between those two dates based on days in the week. "ww" on the other hand figures out the difference based on the weeks those dates fall in acording to the week of the year method.

This month is a particularly good example. It started on a saturday and ends on a Monday, so the math gos something like this:
"w": 31 days/7 = 4.43 rounds down to 4 weeks
"ww": May 1 is in week 18 of the year, may 31 is in week 23, 23 - 8 = 5 weeks difference

I agree that the descriptions on other sites that I saw were misleading (they all looked copied from the MS website, so I gues they are ultimately to blame). To my knowledge there is no built in datePart for "work week days".

Although if you search through the forums for the past month you may find my little shipping calculator function tat managed to do a DateAdd based on work days...though I warn you the code i exceptionally nasty as I was compressing 20+ lines down into about 3 or 4 :p

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top