glenwr
asked on
DATEADD() Function To Find Closest Day Of Week, One Month Later.
Hey Experts,
I have a date challenge. I'm writing an ASP application that uses dateadd() to get the date of the next month and that is fine. I use this:
nextmonth = dateadd("m",1,date)
My issue is that I need the nextmonth value to be on the same day of the week as the starting date, for example, today is Thursday, 8/5/2004, the above function returns Saturday, 9/5/2004. I really need to calculate the closest Thursday to the nextmonth value which would be 9/2/2004.
I hope that someone has solved this issue and can be of help.
Thanks,
Glen
I have a date challenge. I'm writing an ASP application that uses dateadd() to get the date of the next month and that is fine. I use this:
nextmonth = dateadd("m",1,date)
My issue is that I need the nextmonth value to be on the same day of the week as the starting date, for example, today is Thursday, 8/5/2004, the above function returns Saturday, 9/5/2004. I really need to calculate the closest Thursday to the nextmonth value which would be 9/2/2004.
I hope that someone has solved this issue and can be of help.
Thanks,
Glen
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That will work for 4 week months but what about 5 week months?
<%
datetocheck=date
response.write getFriday(datetocheck,week day(dateto check))
function getFriday(strdate,daytofin d)
olddate=dateadd("m",1,strd ate)
tmpdate= year(olddate) & "/" & month(olddate) & "/" & "1"
for count=1 to 7
tmpdate=dateadd("d",-count ,tmpdate)
if daytofind=weekday(tmpdate) then exit for
next
getFriday=formatdatetime(t mpdate,1)
end function
%>
<%
datetocheck=date
response.write getFriday(datetocheck,week
function getFriday(strdate,daytofin
olddate=dateadd("m",1,strd
tmpdate= year(olddate) & "/" & month(olddate) & "/" & "1"
for count=1 to 7
tmpdate=dateadd("d",-count
if daytofind=weekday(tmpdate)
next
getFriday=formatdatetime(t
end function
%>
ASKER
You are right Gary.
I have been using:
nextmonth = dateadd("ww",4,date)
The application is working with the recurrence patterns in the Outlook Calendar. I'm not sure that each monthly recurrence is exactly 4 weeks apart. For example, if an appointment is booked on the 5th Thursday of a month (which can happen), then if the next month only has 4 Thursdays, will Outlook book the appointment on the 4th Thursday on next month?
Let me test the solutions. You guys rock!
I have been using:
nextmonth = dateadd("ww",4,date)
The application is working with the recurrence patterns in the Outlook Calendar. I'm not sure that each monthly recurrence is exactly 4 weeks apart. For example, if an appointment is booked on the 5th Thursday of a month (which can happen), then if the next month only has 4 Thursdays, will Outlook book the appointment on the 4th Thursday on next month?
Let me test the solutions. You guys rock!
ASKER
OK Guys,
I've tested the code snipets that were posted. When I bounce the results against the Outlook calendar, what I really need to do is find the nth occurence of the date/day in the next month. For example, today is the first Thursday of August, the next date I will need is the first Thursday in September, this can be done with dateadd("ww",4,date)
. But, in the case where my starting date is the fifth Thursday of a month, I will need the last Thursday of the next month, if there are less than 5 Thursdays.
Now that I have enough information to be specific, any ideas?
Thanks Again
I've tested the code snipets that were posted. When I bounce the results against the Outlook calendar, what I really need to do is find the nth occurence of the date/day in the next month. For example, today is the first Thursday of August, the next date I will need is the first Thursday in September, this can be done with dateadd("ww",4,date)
. But, in the case where my starting date is the fifth Thursday of a month, I will need the last Thursday of the next month, if there are less than 5 Thursdays.
Now that I have enough information to be specific, any ideas?
Thanks Again
Try this out
Function DayOccurence(dteDate)
Dim intN
intN = int(day(dtedate) / 7) + 1
Select Case intN
Case 1
DayOccurence = "First"
Case 2
DayOccurence = "Second"
Case 3
DayOccurence = "Third"
Case 4
DayOccurence = "Fourth"
Case 5
DayOccurence = "Fifth"
End Select
End Function
Function DayOccurence(dteDate)
Dim intN
intN = int(day(dtedate) / 7) + 1
Select Case intN
Case 1
DayOccurence = "First"
Case 2
DayOccurence = "Second"
Case 3
DayOccurence = "Third"
Case 4
DayOccurence = "Fourth"
Case 5
DayOccurence = "Fifth"
End Select
End Function
Sorry didn't read the whole question.
Try this one.
Dim dteStart, isLastWeek, intWeekNo, intNextMonth
dteStart = date()
intNextMonth = dateadd("mm",1 ,dteStart)
intWeekNo = int(day(dteStart) / 7) + 1
dteEnd = dateadd("ww",intWeekNo ,dteStart)
DO WHILE month(dteEnd) <> intNextMonth
dteEnd = dateadd("ww",-1,dteEnd)
LOOP
Dim dteStart, isLastWeek, intWeekNo, intNextMonth
dteStart = date()
intNextMonth = dateadd("mm",1 ,dteStart)
intWeekNo = int(day(dteStart) / 7) + 1
dteEnd = dateadd("ww",intWeekNo ,dteStart)
DO WHILE month(dteEnd) <> intNextMonth
dteEnd = dateadd("ww",-1,dteEnd)
LOOP
Hello @glenwr
Have you tested my post ?? if yes.. what output u got ??
neeraj523
Have you tested my post ?? if yes.. what output u got ??
neeraj523
nextmonth = dateadd("d",28,date)