473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting the Date of the nth Weekday in Every Month

Hi

Hopefully the subject says it all. Given a weekday, (1 to 7) and an
occurrence with in a month, (1 to 4), I need to ascertain the date on which
it first occurs in any given month and year.

For example, what is the date of the third Tuesday in October 2004.

Has anyone done this before? Would be grateful for some assistance.

Regards

David
Jul 19 '05 #1
2 6598
Essentially, you need to first find out what day is the first of the month.
Then figure out how many days to the next "x"day and then add it to
mm/1/yyyy

nYear = 2004
nMonth = 2

' xDay = the day you want to find out the date for
xDay = vbTuesDay

' Get the first of the month as a date
yourDate = DateSerial(nYear, nMonth, 1)

' Let us say first of the week is a Sunday (as in the US)
firstOfWeek = vbSunday

' Find out what day is that
nDay = WeekDay(yourDate, firstOfWeek)

' First "xDay" will be as follows
' See if the xDay is beyond the first of the month
' Else you have to go the xDay of next week
If (xDay - nDay) >= 0 Then
firstXDate = DateSerial(nYear, nMonth, 1 + (xDay - nDay))
Else
firstXDate = DateSerial(nYear, nMonth, 1 + (xDay - nDay) + 7)
End If

Hope fully that helps your "date problem" :)

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"David Morgan" <da***@davidmorgan.me.uk> wrote in message
news:e3****************@TK2MSFTNGP10.phx.gbl...
Hi

Hopefully the subject says it all. Given a weekday, (1 to 7) and an
occurrence with in a month, (1 to 4), I need to ascertain the date on which it first occurs in any given month and year.

For example, what is the date of the third Tuesday in October 2004.

Has anyone done this before? Would be grateful for some assistance.

Regards

David

Jul 19 '05 #2
A small change, for Nth xDay:

' Nth "xDay" will be as follows
' See if the xDay is beyond the first of the month
' Else you have to go the xDay of next week
If (xDay - nDay) >= 0 Then
firstXDate = DateSerial(nYear, nMonth, 1 + (xDay - nDay) + (N-1) * 7)
Else
firstXDate = DateSerial(nYear, nMonth, 1 + (xDay - nDay) + (N * 7))
End If
--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Manohar Kamath [MVP]" <mk*****@TAKETHISOUTkamath.com> wrote in message
news:O5****************@TK2MSFTNGP10.phx.gbl...
Essentially, you need to first find out what day is the first of the month. Then figure out how many days to the next "x"day and then add it to
mm/1/yyyy

nYear = 2004
nMonth = 2

' xDay = the day you want to find out the date for
xDay = vbTuesDay

' Get the first of the month as a date
yourDate = DateSerial(nYear, nMonth, 1)

' Let us say first of the week is a Sunday (as in the US)
firstOfWeek = vbSunday

' Find out what day is that
nDay = WeekDay(yourDate, firstOfWeek)

' First "xDay" will be as follows
' See if the xDay is beyond the first of the month
' Else you have to go the xDay of next week
If (xDay - nDay) >= 0 Then
firstXDate = DateSerial(nYear, nMonth, 1 + (xDay - nDay))
Else
firstXDate = DateSerial(nYear, nMonth, 1 + (xDay - nDay) + 7)
End If

Hope fully that helps your "date problem" :)

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"David Morgan" <da***@davidmorgan.me.uk> wrote in message
news:e3****************@TK2MSFTNGP10.phx.gbl...
Hi

Hopefully the subject says it all. Given a weekday, (1 to 7) and an
occurrence with in a month, (1 to 4), I need to ascertain the date on

which
it first occurs in any given month and year.

For example, what is the date of the third Tuesday in October 2004.

Has anyone done this before? Would be grateful for some assistance.

Regards

David


Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: michaaal | last post by:
How can I get a list of the date of every friday of the year? I realize this code is wrong, but maybe you'll see what I am after... Sub GetFridayList() For x = 1 to len(Date(2004)) if...
7
by: Bob | last post by:
I passed in a DateTime object, and want to get its end date of the month. Is there a easy way to do it? Thanks.
6
by: thamaraikeeran | last post by:
i want to display the selected date from the month calendar in the textbox.please tell me how to do?plz reply quikly.
2
by: arulkumara | last post by:
Hi, I want to insert a particular row into a table for every month of beginnging date like 1-11-2007,1-12-2007. for that which one is best triggres or batch jobs. please...
2
by: p655279 | last post by:
Dear All, Does anyone know how to extract first business date of each month in a DB2 SQL? many many thanks
1
by: rahia307 | last post by:
hi i want to create email function every month at 25 date. how can i create it. if any one have any idea about it help me.
9
vikas251074
by: vikas251074 | last post by:
I am not getting date value in spite of my good effort. This code was working in my last office where I work. Now I am trying to work at my home pc. but not getting date value. Any can help me why...
1
by: Usman ahmed | last post by:
how do i set a default date of one month from now, this is for the return date field
11
by: mrzebo1 | last post by:
I need a vba procedure that outputs the the date of every monday of the year. The output would look like 01/03/2011 01/10/2011 01/17/2011 . . . 12/26/2011
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.