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

Convert Week Number To Date Range

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 that there are different start days of the week so I would
presume any function would provide that facility. Hope you can help
Mark
Nov 12 '05 #1
4 22628
ka*******@hotmail.com (Mark) wrote:
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 that there are different start days of the week so I would
presume any function would provide that facility. Hope you can help
Mark


Hi Mark,

This is a function I use to create a table which gives week number agains a
date range. You might be able to adapt this for your purposes. Note that
the date range in the code dictates the start day for each week (currently
a Monday).

HTH - Keith.
www.keithwilby.org.uk

Public Sub libMakeDate()

Dim db As Database
Dim td As TableDef
Dim fd As Field
Dim rs As Recordset
Dim dtmdate As Date

Set db = CurrentDb

' Create a new table
Set td = db.CreateTableDef("tblDate")

' Create a new field
Set fd = New Field
fd.name = "Date"
fd.Type = dbDate

' Add the field to the table
td.Fields.Append fd

' Create a new field
Set fd = New Field
fd.name = "WeekNo"
fd.Type = dbText

' Add the field to the table
td.Fields.Append fd

' Add the table to the database
db.TableDefs.Append td

' Open the table
Set rs = td.OpenRecordset

' Add dates and weekno
For dtmdate = #10/27/02# To #12/31/10# Step 7
rs.AddNew
rs!Date = dtmdate
rs!WeekNo = Year(dtmdate) Mod 10 & Right("0" & Format(dtmdate,
"ww"), 2)
rs.Update
Next
rs.Close

End Sub
Nov 12 '05 #2

Function WeekNoToDate(StartDate As Date, WeekNo As Double) As String

Dim FirstDate As Date, SecondDate As Date

If WeekNo > 52 Or WeekNo < -52 Then
MsgBox "Maximum week no is + or - 52", vbCritical
Exit Function
End If

If Int(WeekNo) <> WeekNo Then
MsgBox "Week No must be a whole number", vbCritical
Exit Function
End If

FirstDate = DateAdd("ww", WeekNo, StartDate)
SecondDate = DateAdd("d", FirstDate, 6)
WeekNoToDate = "From:" & Format(FirstDate, "Medium Date") & " to:" &
Format(SecondDate, "Medium Date")

End Function

Watch how the StartDate is entered

WeekNoToDate(#1 apr 03#, 50) is OK
WeekNoToDate(#1/4/03#, 50) gives From:20-Dec-03 to:26-Dec-03 because it
assumes the date is 4th January.
Dates are all daft. Wouldn't it be much more logical to go yyyy/mm/dd. The
Brits have got it completely arse about face, and the Americans are even
more illogical with mm/dd/yyyy.

Phil
"Keith Wilby" <ke*********@AwayWithYerCrap.com> wrote in message
news:Xn************************@10.15.188.42...
ka*******@hotmail.com (Mark) wrote:
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 that there are different start days of the week so I would
presume any function would provide that facility. Hope you can help
Mark

Hi Mark,

This is a function I use to create a table which gives week number agains

a date range. You might be able to adapt this for your purposes. Note that
the date range in the code dictates the start day for each week (currently
a Monday).

HTH - Keith.
www.keithwilby.org.uk

Public Sub libMakeDate()

Dim db As Database
Dim td As TableDef
Dim fd As Field
Dim rs As Recordset
Dim dtmdate As Date

Set db = CurrentDb

' Create a new table
Set td = db.CreateTableDef("tblDate")

' Create a new field
Set fd = New Field
fd.name = "Date"
fd.Type = dbDate

' Add the field to the table
td.Fields.Append fd

' Create a new field
Set fd = New Field
fd.name = "WeekNo"
fd.Type = dbText

' Add the field to the table
td.Fields.Append fd

' Add the table to the database
db.TableDefs.Append td

' Open the table
Set rs = td.OpenRecordset

' Add dates and weekno
For dtmdate = #10/27/02# To #12/31/10# Step 7
rs.AddNew
rs!Date = dtmdate
rs!WeekNo = Year(dtmdate) Mod 10 & Right("0" & Format(dtmdate,
"ww"), 2)
rs.Update
Next
rs.Close

End Sub

Nov 12 '05 #3
Thank you very much both of you. I will have a look at them now and
tell you how I got on
Mark
"Phil Stanton" <ph**@stantonfamily.co.uk> wrote in message news:<3f***********************@mercury.nildram.ne t>...
Function WeekNoToDate(StartDate As Date, WeekNo As Double) As String

Dim FirstDate As Date, SecondDate As Date

If WeekNo > 52 Or WeekNo < -52 Then
MsgBox "Maximum week no is + or - 52", vbCritical
Exit Function
End If

If Int(WeekNo) <> WeekNo Then
MsgBox "Week No must be a whole number", vbCritical
Exit Function
End If

FirstDate = DateAdd("ww", WeekNo, StartDate)
SecondDate = DateAdd("d", FirstDate, 6)
WeekNoToDate = "From:" & Format(FirstDate, "Medium Date") & " to:" &
Format(SecondDate, "Medium Date")

End Function

Watch how the StartDate is entered

WeekNoToDate(#1 apr 03#, 50) is OK
WeekNoToDate(#1/4/03#, 50) gives From:20-Dec-03 to:26-Dec-03 because it
assumes the date is 4th January.
Dates are all daft. Wouldn't it be much more logical to go yyyy/mm/dd. The
Brits have got it completely arse about face, and the Americans are even
more illogical with mm/dd/yyyy.

Phil
"Keith Wilby" <ke*********@AwayWithYerCrap.com> wrote in message
news:Xn************************@10.15.188.42...
ka*******@hotmail.com (Mark) wrote:
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 that there are different start days of the week so I would
presume any function would provide that facility. Hope you can help
Mark


Hi Mark,

This is a function I use to create a table which gives week number agains

a
date range. You might be able to adapt this for your purposes. Note that
the date range in the code dictates the start day for each week (currently
a Monday).

HTH - Keith.
www.keithwilby.org.uk

Public Sub libMakeDate()

Dim db As Database
Dim td As TableDef
Dim fd As Field
Dim rs As Recordset
Dim dtmdate As Date

Set db = CurrentDb

' Create a new table
Set td = db.CreateTableDef("tblDate")

' Create a new field
Set fd = New Field
fd.name = "Date"
fd.Type = dbDate

' Add the field to the table
td.Fields.Append fd

' Create a new field
Set fd = New Field
fd.name = "WeekNo"
fd.Type = dbText

' Add the field to the table
td.Fields.Append fd

' Add the table to the database
db.TableDefs.Append td

' Open the table
Set rs = td.OpenRecordset

' Add dates and weekno
For dtmdate = #10/27/02# To #12/31/10# Step 7
rs.AddNew
rs!Date = dtmdate
rs!WeekNo = Year(dtmdate) Mod 10 & Right("0" & Format(dtmdate,
"ww"), 2)
rs.Update
Next
rs.Close

End Sub

Nov 12 '05 #4
On 27 Nov 2003 06:54:26 -0800, ka*******@hotmail.com (Mark) wrote:
I have been trying to convert the week number to a range of dates that
I can use.


This is dead simple if you use a table instead of code. (And you
*should* use a table instead of code. Business data belongs in
tables.)

--
Mike Sherrill
Information Management Systems
Nov 12 '05 #5

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

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
2
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
14
by: deko | last post by:
This runs, but does not narrow to current week. suggestions appreciated! SELECT lngEid, dtmApptDate, Subject, Appt_ID FROM qry002 WHERE (dtmApptDate BETWEEN DateAdd("d",-weekday()+2,) And...
9
by: PamelaDV | last post by:
I have two problems, actually. I am looking to see if there is a function that will return the day of the week (Monday, Tuesday, Wednesday, etc...) from a date. For instance 2/27/04 is a Friday....
3
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the...
2
by: planetthoughtful | last post by:
Hi All, I'm building some reports in Acc97 and using a custom calendar form to allow users to pick dates with which to report. I'm wondering if there's an easy way in code to be able to...
3
by: Manekurt | last post by:
Hello, does anyone knows how to convert a date, into a number of week of an year? for example mm/dd/yyyy 01/04/2006 convert to 1 (week number 1 of the year) Thanik you
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...
7
by: derekdeben | last post by:
Hi, I have created a report that totals the number of days it took a product to ship by percentage by a date range for a specific location. My data comes from a query with the following headers: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.