473,405 Members | 2,261 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,405 software developers and data experts.

WeekOfMonth Function

Hi Programmers,

I am trying to figure out how to create a function what will return the Week of the Month when a date is past in.

Looking at the month of July for 2004. Sunday is the first day of the week, Saturday the last day of the week.
Passing the date July 14, 2004 to the WeekOfMonth function will return a 2 because July 14, 2004 is the second Wednesday of the
Month.
Passing the date July 15, 2004 to the WeekOfMonth function will return a 3 because July 15, 2004 is the third Thursday of the Month.
Passing the date July 31, 2004 to the WeekOfMonth function will return a 5 because July 31, 2004 is the fifth Saturday of the Month.

Can anyone give me a clue of how to create this WeekOfMonth function?

Thanks,
Barry
Nov 13 '05 #1
6 3492
Which are you wanting? You stated that the first day of the week is Sunday,
following that logic, July 15, 2004 should return 2 and July 31, 2004 should
return 4 since the first week in July starts on Sunday, July 4, 2004. Do you
want the numbers above returned or the 3 and 5 you mentioned in your
message?

--
Wayne Morgan
MS Access MVP
"Barry Wright" <ba*****************@NOSPAMrogers.com> wrote in message
news:OA***************@news04.bloor.is.net.cable.r ogers.com...
Hi Programmers,

I am trying to figure out how to create a function what will return the Week of the Month when a date is past in.
Looking at the month of July for 2004. Sunday is the first day of the week, Saturday the last day of the week. Passing the date July 14, 2004 to the WeekOfMonth function will return a 2 because July 14, 2004 is the second Wednesday of the Month.
Passing the date July 15, 2004 to the WeekOfMonth function will return a 3 because July 15, 2004 is the third Thursday of the Month. Passing the date July 31, 2004 to the WeekOfMonth function will return a 5 because July 31, 2004 is the fifth Saturday of the Month.
Can anyone give me a clue of how to create this WeekOfMonth function?

Thanks,
Barry

Nov 13 '05 #2
Wayne thanks for the response. The answer to you question is: I would like 3 and 5 returned.

More examples: taking the month July/2004, if you pass in July 1, 2004 the function will return 1 because it is the first Thursday
of the month, if you pass in July 8, 2004 the function will return 2 because it is the second Thursday of the month, if you pass in
July 7, 2004 it will return 1 because it is the first Wednesday of the month, if you pass in July 29, 2004 the function will return
5 because it is the fifth Thursday of the Month.

Thanks
Barry

"Wayne Morgan" <co***************************@hotmail.com> wrote in message news:Bb*******************@newssvr23.news.prodigy. com...
Which are you wanting? You stated that the first day of the week is Sunday,
following that logic, July 15, 2004 should return 2 and July 31, 2004 should
return 4 since the first week in July starts on Sunday, July 4, 2004. Do you
want the numbers above returned or the 3 and 5 you mentioned in your
message?

--
Wayne Morgan
MS Access MVP
"Barry Wright" <ba*****************@NOSPAMrogers.com> wrote in message
news:OA***************@news04.bloor.is.net.cable.r ogers.com...
Hi Programmers,

I am trying to figure out how to create a function what will return the Week of the Month when a date is past in.
Looking at the month of July for 2004. Sunday is the first day of the week, Saturday the last day of the week. Passing the date July 14, 2004 to the WeekOfMonth function will return a 2 because July 14, 2004 is the second Wednesday of the Month.
Passing the date July 15, 2004 to the WeekOfMonth function will return a 3 because July 15, 2004 is the third Thursday of the Month. Passing the date July 31, 2004 to the WeekOfMonth function will return a 5 because July 31, 2004 is the fifth Saturday of the Month.
Can anyone give me a clue of how to create this WeekOfMonth function?

Thanks,
Barry


Nov 13 '05 #3
Public Function WeekOfMonth(dteInputDate As Date) As Integer
Dim intDate As Integer
intDate = Day(dteInputDate)
Select Case intDate
Case 1 To 7
WeekOfMonth = 1
Case 8 To 14
WeekOfMonth = 2
Case 15 To 21
WeekOfMonth = 3
Case 22 To 28
WeekOfMonth = 4
Case 29 To 31
WeekOfMonth = 5
Case Else
WeekOfMonth = 0
End Select
End Function

--
Wayne Morgan
MS Access MVP
"Barry Wright" <ba*****************@NOSPAMrogers.com> wrote in message
news:lP************@news04.bloor.is.net.cable.roge rs.com...
Wayne thanks for the response. The answer to you question is: I would like 3 and 5 returned.
More examples: taking the month July/2004, if you pass in July 1, 2004 the function will return 1 because it is the first Thursday of the month, if you pass in July 8, 2004 the function will return 2 because it is the second Thursday of the month, if you pass in July 7, 2004 it will return 1 because it is the first Wednesday of the month, if you pass in July 29, 2004 the function will return 5 because it is the fifth Thursday of the Month.

Thanks
Barry

"Wayne Morgan" <co***************************@hotmail.com> wrote in message news:Bb*******************@newssvr23.news.prodigy. com... Which are you wanting? You stated that the first day of the week is Sunday, following that logic, July 15, 2004 should return 2 and July 31, 2004 should return 4 since the first week in July starts on Sunday, July 4, 2004. Do you want the numbers above returned or the 3 and 5 you mentioned in your
message?

--
Wayne Morgan
MS Access MVP
"Barry Wright" <ba*****************@NOSPAMrogers.com> wrote in message
news:OA***************@news04.bloor.is.net.cable.r ogers.com...
Hi Programmers,

I am trying to figure out how to create a function what will return the Week of the Month when a date is past in.

Looking at the month of July for 2004. Sunday is the first day of the

week, Saturday the last day of the week.
Passing the date July 14, 2004 to the WeekOfMonth function will return a

2 because July 14, 2004 is the second Wednesday of the
Month.
Passing the date July 15, 2004 to the WeekOfMonth function will return a
3 because July 15, 2004 is the third Thursday of the Month.
Passing the date July 31, 2004 to the WeekOfMonth function will return a
5 because July 31, 2004 is the fifth Saturday of the Month.

Can anyone give me a clue of how to create this WeekOfMonth function?

Thanks,
Barry


Nov 13 '05 #4
Actually, this is much shorter. It relies on the value of True being treated
as -1. "\" is integer division.

Public Function WeekOfMonth(dteInputDate As Date) As Integer
Dim intDate As Integer
intDate = Day(dteInputDate)
WeekOfMonth = (intDate \ 7) + 1 + (intDate Mod 7 = 0)
End Function

--
Wayne Morgan
MS Access MVP
"Barry Wright" <ba*****************@NOSPAMrogers.com> wrote in message
news:lP************@news04.bloor.is.net.cable.roge rs.com...
Wayne thanks for the response. The answer to you question is: I would like 3 and 5 returned.
More examples: taking the month July/2004, if you pass in July 1, 2004 the function will return 1 because it is the first Thursday of the month, if you pass in July 8, 2004 the function will return 2 because it is the second Thursday of the month, if you pass in July 7, 2004 it will return 1 because it is the first Wednesday of the month, if you pass in July 29, 2004 the function will return 5 because it is the fifth Thursday of the Month.

Thanks
Barry

"Wayne Morgan" <co***************************@hotmail.com> wrote in message news:Bb*******************@newssvr23.news.prodigy. com... Which are you wanting? You stated that the first day of the week is Sunday, following that logic, July 15, 2004 should return 2 and July 31, 2004 should return 4 since the first week in July starts on Sunday, July 4, 2004. Do you want the numbers above returned or the 3 and 5 you mentioned in your
message?

--
Wayne Morgan
MS Access MVP
"Barry Wright" <ba*****************@NOSPAMrogers.com> wrote in message
news:OA***************@news04.bloor.is.net.cable.r ogers.com...
Hi Programmers,

I am trying to figure out how to create a function what will return the Week of the Month when a date is past in.

Looking at the month of July for 2004. Sunday is the first day of the

week, Saturday the last day of the week.
Passing the date July 14, 2004 to the WeekOfMonth function will return a

2 because July 14, 2004 is the second Wednesday of the
Month.
Passing the date July 15, 2004 to the WeekOfMonth function will return a
3 because July 15, 2004 is the third Thursday of the Month.
Passing the date July 31, 2004 to the WeekOfMonth function will return a
5 because July 31, 2004 is the fifth Saturday of the Month.

Can anyone give me a clue of how to create this WeekOfMonth function?

Thanks,
Barry


Nov 13 '05 #5
"Wayne Morgan" <co***************************@hotmail.com> wrote in message news:<Xt*************@newssvr24.news.prodigy.com>. ..
Actually, this is much shorter. It relies on the value of True being treated
as -1. "\" is integer division.

Public Function WeekOfMonth(dteInputDate As Date) As Integer
Dim intDate As Integer
intDate = Day(dteInputDate)
WeekOfMonth = (intDate \ 7) + 1 + (intDate Mod 7 = 0)
End Function

--
Wayne Morgan
MS Access MVP


That's a great solution Wayne. To get around the 'True' problem perhaps use:

WeekOfMonth = (intDate - 1) \ 7 + 1

James A. Fortune
Nov 13 '05 #6
Thanks Guys,
Your soluntions worked superbly!
I also came up with a solution of my own but yours were 20% faster.

Regards
Barry Wright

Public Function WeekOfMonth(vDate As Date) As Long

'Barry Wright Solution
Dim vMonth As Long 'Month of Date Passed In
Dim vMonth1 As Long 'Calculated Month

'Initialize variables
vMonth = Month(vDate)
vMonth1 = Month(vDate)
WeekOfMonth = 0

Do While vMonth = vMonth1
vDate = DateAdd("d", -7, vDate)
vMonth1 = Month(vDate)
WeekOfMonth = WeekOfMonth + 1
Loop

End Function


"James Fortune" <ja******@oakland.edu> wrote in message news:a6**************************@posting.google.c om...
"Wayne Morgan" <co***************************@hotmail.com> wrote in message news:<Xt*************@newssvr24.news.prodigy.com>. ..
Actually, this is much shorter. It relies on the value of True being treated
as -1. "\" is integer division.

Public Function WeekOfMonth(dteInputDate As Date) As Integer
Dim intDate As Integer
intDate = Day(dteInputDate)
WeekOfMonth = (intDate \ 7) + 1 + (intDate Mod 7 = 0)
End Function

--
Wayne Morgan
MS Access MVP


That's a great solution Wayne. To get around the 'True' problem perhaps use:

WeekOfMonth = (intDate - 1) \ 7 + 1

James A. Fortune
Nov 13 '05 #7

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.