473,434 Members | 1,484 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,434 software developers and data experts.

Given a week Number, how do I calculate the Monday of that week?

Given a week Number, how do I calculate the date that for the Monday
of that week?

Oct 9 '08 #1
4 10799
On Thu, 9 Oct 2008 00:53:32 -0700 (PDT), Vince <Vi*******@gmail.com>
wrote:

Use the DatePart function with "w" to get the weekday of the given
date. Then use DateAdd to subtract a few days to arrive at Monday,
which is day 2 if you don't specify a FirstDayOfWeek argument.

-Tom.
Microsoft Access MVP

>Given a week Number, how do I calculate the date that for the Monday
of that week?
Oct 9 '08 #2
Vince wrote:
Given a week Number, how do I calculate the date that for the Monday
of that week?
You could copy this code into a module and play around with it. Change
intWeekNum in GetTheMonday for the number of weeks to add to test. You
can see this year's (2008) first Monday week is before the first of the
year so make adjustments as required.

Run GetTheMonday to cycle thru 2006-2008. Check results in Immediate
window.

Private Sub GetTheMonday
Dim datMonday As Date
Dim intFor as Integer
Dim intWeekNum As Integer

intWeekNum = 1 'week number to calc date from

'show in debug window the first monday for years 2006-2008
For intFor = 2006 to 2008
'get date of first monday in year 2008
Debug.Print MondayWeek(intFor,intWeekNum)
Next

endif

Public Function MondayWeek(intYear As integer, _
intWeek As Integer) As Date
Dim dat As Date
Dim intD As Integer

'calc first day of year of datFld
dat = DateSerial(intYear, 1, 1)
intD = WeekDay(dat)
Select Case intD
Case 1
dat = dat + 1
Case Else
'subtract 2 to get first Monday
dat = dat - (intD - 2)
End Select
'if datYear is
'2006 1/2/2006 is first monday
'2007 1/1/2007 is first monday
'2008 12/31/2007 is first monday

'add number of weeks minus 1 to first monday
MondayWeek = DateAdd("ww", intWeek - 1, dat)
End Function

Gronlandic Edit
http://www.youtube.com/watch?v=HBfgQvM7wtE

Oct 9 '08 #3
On Oct 9, 7:34*am, Salad <o...@vinegar.comwrote:
Vince wrote:
Given a week Number, how do I calculate the date that for theMonday
of that week?

You could copy this code into a module and play around with it. *Change
intWeekNum in GetTheMonday for the number of weeks to add to test. *You
can see this year's (2008) firstMondayweek is before the first of the
year so make adjustments as required.

Run GetTheMonday to cycle thru 2006-2008. *Check results in Immediate
window.

Private Sub GetTheMonday
* * * * Dim datMonday As Date
* * * * Dim intFor as Integer
* * * * Dim intWeekNum As Integer

* * * * intWeekNum = 1 *'week number to calc date from

* * * * *'show in debug window the firstmondayfor years 2006-2008
* * * * For intFor = 2006 to 2008
* * * * * 'get date of firstmondayin year 2008
* * * * * Debug.Print MondayWeek(intFor,intWeekNum)
* * * * Next

endif

Public Function MondayWeek(intYear As integer, _
* * * * intWeek As Integer) As Date
* * *Dim dat As Date
* * *Dim intD As Integer

* * *'calc first day of year of datFld
* * *dat = DateSerial(intYear, 1, 1)
* * *intD = WeekDay(dat)
* * *Select Case intD
* * *Case 1
* * * * *dat = dat + 1
* * *Case Else
* * * * 'subtract 2 to get firstMonday
* * * * *dat = dat - (intD - 2)
* * *End Select
* * *'if datYear is
* * * * *'2006 1/2/2006 is firstmonday
* * * * *'2007 1/1/2007 is firstmonday
* * * * *'2008 12/31/2007 is firstmonday

* * *'add number of weeks minus 1 to firstmonday
* * *MondayWeek = DateAdd("ww", intWeek - 1, dat)
End Function

Gronlandic Edithttp://www.youtube.com/watch?v=HBfgQvM7wtE
thanks !! the Function MondayWeek worked for me!
Oct 13 '08 #4
Vince wrote:
On Oct 9, 7:34 am, Salad <o...@vinegar.comwrote:
>>Vince wrote:
>>>Given a week Number, how do I calculate the date that for theMonday
of that week?

You could copy this code into a module and play around with it. Change
intWeekNum in GetTheMonday for the number of weeks to add to test. You
can see this year's (2008) firstMondayweek is before the first of the
year so make adjustments as required.

Run GetTheMonday to cycle thru 2006-2008. Check results in Immediate
window.

Private Sub GetTheMonday
Dim datMonday As Date
Dim intFor as Integer
Dim intWeekNum As Integer

intWeekNum = 1 'week number to calc date from

'show in debug window the firstmondayfor years 2006-2008
For intFor = 2006 to 2008
'get date of firstmondayin year 2008
Debug.Print MondayWeek(intFor,intWeekNum)
Next

endif

Public Function MondayWeek(intYear As integer, _
intWeek As Integer) As Date
Dim dat As Date
Dim intD As Integer

'calc first day of year of datFld
dat = DateSerial(intYear, 1, 1)
intD = WeekDay(dat)
Select Case intD
Case 1
dat = dat + 1
Case Else
'subtract 2 to get firstMonday
dat = dat - (intD - 2)
End Select
'if datYear is
'2006 1/2/2006 is firstmonday
'2007 1/1/2007 is firstmonday
'2008 12/31/2007 is firstmonday

'add number of weeks minus 1 to firstmonday
MondayWeek = DateAdd("ww", intWeek - 1, dat)
End Function

Gronlandic Edithttp://www.youtube.com/watch?v=HBfgQvM7wtE


thanks !! the Function MondayWeek worked for me!
You're welcome. I'm still a bit concerned about a Monday falling into
the previous year but I suppose you could check if the Monday is in a
prior year (if it shouldn't be, although maybe it should) and add 7 to
it. Ex:
MondayWeek = DateAdd("ww", intWeek - 1, dat)
'next line for testing if you need to increment by 7
If Year(dat) < intYear then MondayWeek = MondayWeek + 7
If you don't want the first Monday to fall in a prior year and do add 7
you might want to modify the code in GetTheMonday cycle through 1900 to
2008 to see if by adding 7 you end up in the next year by doing a
Debug.Print MondayWeek if you need/want to add 7 days. I might also
want to check this for week 52.

intWeekNum = 1 'week number to calc date from
For intFor = 1900 to 2008
and
intWeekNum = 52 'week number to calc date from
For intFor = 1900 to 2008
then in function MondayWeek
'print the years
MondayWeek = DateAdd("ww", intWeek - 1, dat)
If Year(dat) <intYear then Debug.print intYear;MondayWeek
You might find some anomolies in cycling thru all those years.

Oct 13 '08 #5

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

Similar topics

11
by: Matt | last post by:
My ASP page allows the user select the date, and it will display the report during that week of that date. My question is when the program query database, I need to know the beginning date and...
4
by: Shahid Juma | last post by:
Hi, I am trying to figure out a way to get the week number of the current week. I know this is relatively easy using the calendar year (starting from January to now). However, I want to do it so...
7
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code...
4
by: Mark | last post by:
Hi I have been trying to convert the week number to a range of dates that I can use. It should be fairly simple for you guru's out there but for us mere mortals it is beyond our grasp. I know...
2
by: Rustan | last post by:
Hi Im using GregorianCalendar to find out the current years week numbers. When the user chooses a week number in a dropdown i want to show that week in a table with the corresponding dates. For...
3
by: Mal | last post by:
Hello, Any advice on a function to convert a given week number to a date? Ideally I'd like the first day of that week. I'm using this to compare year to year....using the week number as the...
3
by: Tim Chase | last post by:
I've been trying to come up with a good algorithm for determining the starting and ending dates given the week number (as defined by the strftime("%W") function). My preference would be for a...
13
by: Zahed | last post by:
Can anyone help. Need to calculate Week number of a date. But Week must start on a Monday not a Sunday. Excel provides a function where WeekNum("Date",1) starts week on Sunday...
5
by: Michael R. Copeland | last post by:
I need to determine a given date's week number, but my searches of google only confuse and frustrate me: there are many apparent formulas (which are pretty complex) and they all seem to require...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.