DATEADD() Function To Find Closest Day Of Week, One Month Later.

Posted on 2004-08-05
Last Modified: 2012-08-13
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.

Question by:glenwr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 14

Expert Comment

ID: 11725122
why not adding 4 weeks?

nextmonth = dateadd("d",28,date)
LVL 22

Accepted Solution

neeraj523 earned 250 total points
ID: 11725210

Try this

currDate = "05-Aug-04"
currDay = WeekDay(currDate)

nextDate = DateAdd("m",1,currDate)
nextDay = WeekDay(nextDate)

if nextDay >= currDay Then
      nextDate = DateAdd("d",-(nextDay-currDay),nextDate)
      nextDate = DateAdd("d",(currDay-nextDay),nextDate)
End if

LVL 58

Expert Comment

ID: 11725221
That will work for 4 week months but what about 5 week months?

response.write getFriday(datetocheck,weekday(datetocheck))

function getFriday(strdate,daytofind)
   tmpdate= year(olddate) & "/" & month(olddate) & "/" & "1"
   for count=1 to 7
      if daytofind=weekday(tmpdate) then exit for
end function
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 11725292
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!

Author Comment

ID: 11725847
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

Expert Comment

ID: 11727153
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

Expert Comment

ID: 11727169
Sorry didn't read the whole question.

Expert Comment

ID: 11730860
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)
LVL 22

Expert Comment

ID: 11732886
Hello @glenwr

Have you tested my post ?? if yes.. what output u got ??


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question