471,579 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Week number to date??

Mal
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
common factor.

Mal.
Jan 8 '06 #1
3 16929
On Sun, 08 Jan 2006 03:59:55 GMT, "Mal" <me@myhome.net> wrote:
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
common factor.

Mal.


*not thoroughly tested

Function fGetWeekDate(intWeekNo As Integer, _
Optional intYear As Integer, _
Optional intStartDay As Integer = vbSunday) As Date

Dim dtCheckDate As Date

If intYear = 0 Then intYear = Year(Date)

'get first (intStartDay) of year
dtCheckDate = CDate("1/1/" & intYear)
Do Until WeekDay(dtCheckDate, intStartDay) = intStartDay
dtCheckDate = dtCheckDate + 1
Loop

'adjust if first (intStartDay) of year is week 52 or 53
If DatePart("ww", dtCheckDate, , vbFirstFourDays) >= 52 Then
dtCheckDate = DateAdd("ww", 1, dtCheckDate)
End If

fGetWeekDate = DateAdd("ww", intWeekNo - 1, dtCheckDate)

End Function
Wayne Gillespie
Gosford NSW Australia
Jan 8 '06 #2
This is not as easy as it first appears. It depends on how your week number
is defined, or rather how the week numbering start is defined.

Possible definitions I can think of are:-
Week 1 is the week in which the 1st of January falls
Week 1 is the first full week of the year
Week 1 is the first week of the year which contains four days of the
year.

This is then compounded by how do you define the first day of the week.

IIRC the ISO standard is that Monday is the first day of the week and the
first week of the year is the first week which contains four days of the
year.

In short until you define how your week number and first day of the week are
defined it's not possible to answer the question.
--
Terry Kreft

"Mal" <me@myhome.net> wrote in message
news:%6*****************@newsread3.news.atl.earthl ink.net...
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
common factor.

Mal.

Jan 8 '06 #3
Mal wrote:
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
common factor.

Mal.

Expanding a bit on what others wrote, you can view these results in the
Immediate/Debug window
? format(#1/1/2006#,"ww",vbMonday,vbFirstFourDays)
52
? format(#1/1/2006#,"ww",vbMonday,vbFirstFullWeek)
52
? format(#1/1/2006#,"ww",vbMonday,vbFirstJan1)
1
? format(#1/1/2006#,"ww",vbMonday,vbUseSystem)
1
? weekday(#1/1/2006#,vbSunday)
1
? weekday(#1/1/2006#,vbMonday)
7

1/1/2006 is a Sunday. vbSunday is the default in the Weekday function.
Which day is your default? Sunday, Monday...Saturday?

I guess you'll need to find out what the week number is for the first of
the year. Then if the week day is not the start week day either
increment or decrement the days till you get your start day from the
first of the year. From there you can multiply the number of (weeks-1)
* 7. If the week is 52, you'd want to set the week to 0.

Jan 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by androtech | last post: by
7 posts views Thread by Shuffs | last post: by
9 posts views Thread by Ray | last post: by
2 posts views Thread by Rustan | last post: by
6 posts views Thread by aarklon | last post: by
3 posts views Thread by sbaird | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.