Claes Gustavsson
Claes Gustavsson

Reputation: 5669

display dates based on week number asp classic

I want to display the weeks dates based on a week number that I get from my db. So if I get week=43 then it would display all 7 dates for that week. Like this.

M=23
T=24
W=25
T=26
F=27
S=28
S=29

Have tested with a lot of date formating but I can't get it working. So any input really appreciated, thanks!

Upvotes: 1

Views: 2230

Answers (2)

Claes Gustavsson
Claes Gustavsson

Reputation: 5669

I ended up with this, works perfect!

currentDate = Date
weekNumber=DatePart("ww", currentDate, vbMonday, vbFirstFourDays)
y = Year(Date)

Public Function FirstDayOfWeek(Year, Week)
Dim TempDate

TempDate = DateSerial(Year, 1, 1)
Do Until DatePart("ww", TempDate, vbMonday, vbFirstFourDays) = 1
TempDate = TempDate + 7
Loop

TempDate = TempDate + (7 * (Week - 1))

FirstDayOfWeek = TempDate - Weekday(TempDate, vbMonday) + 1
End Function

Dim startDatum
Dim slutDatum
startDatum = FirstDayOfWeek(y, weekNumber)
slutDatum = startDatum + 6

mon=DatePart("d", startDatum)
tus=DatePart("d", startDatum+1)
wen=DatePart("d", startDatum+2)
tur=DatePart("d", startDatum+3)
fri=DatePart("d", startDatum+4)
sat=DatePart("d", startDatum+5)
sun=DatePart("d", startDatum+6)

Upvotes: 2

Martha
Martha

Reputation: 3854

Well, there isn't really an inverse DatePart() function, so you have to make your own calculations.

dim w, wd, y, m, i
y = Year(Date) '- year of the week in question; I'm using today's date
w = 43
wd = DateAdd("d",w*7,CDate("1/1/" & y)) '- adjust as needed for 1st week of year
m = DateAdd("d",2-Weekday(wd),wd) '- find Monday of week

Hopefully, you can go from there.

Upvotes: 0

Related Questions