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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get next tuesday 1

Status
Not open for further replies.

ecalbert

Programmer
Dec 25, 2004
61
IL
i'm trying to write a function that when passed a date will return the date of the next tuesday

my code below is not working -can someone help

function getnexttuesday(mydate)
iDay = weekday(mydate)
if iday>2 then mydate=mydate+6
iDay = weekday(mydate)
for i=iday to 3
mydate=mydate+1
if weekday(mydate)=3 then exit for
next
end function
 
If you can't think in code, do it on paper - that is the easiest way. What you have is one of 3 situations
Code:
You are on a Tuesday
SMTWTFSSMTWTFS
  ^

You are before a Tuesday      
SMTWTFSSMTWTFS
^

You are after a Tuesday 
SMTWTFSSMTWTFS
     ^
In the first case, you're OK.
In the second case, add difference between that date and Tuesday
In the third case, add 7 minus difference between that date and Tuesday
Code:
function getnexttuesday(startdate)
   dim currdate   
   currdate = startdate
   iDay = weekday (currdate) - 3
   if iDay < 0 then
      currdate = currdate - iDay
   else
      currdate = currdate + 7 - iDay
   end if
   getnexttuesday = currdate
end function
 
[tt]function getnexttuesday(mydate) 'with return properly made
'validate in-param mydate here if needed <<<
getnexttuesday=mydate+(8-weekday(mydate,3)) 'Tuesday start of week: 3
end function[/tt]
 
It also depends on the interpretation of 'next'. If today is Tuesday, some people wrongly interpret it as today (i.e. this: not next), others interpret it as Tuesday week (English way of saying a week on Tuesday).

Mine will give you today if today is Tuesday but it really does depend on what you want and your interpretation of 'next'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top