Any way to get the DATE of the first DAY of the week from a week number? (vbscript)

Imaginer

Diamond Member
Oct 15, 1999
8,076
1
0
I know with datepart you can pull the number of the week of the year from what date it is but what if you want it the other way around?
 

Imaginer

Diamond Member
Oct 15, 1999
8,076
1
0
see with datepart() function, If I use datepart("ww",somedate) it will let me retrieve what week of the year it is in. Like week 32 of the year or week 2 and so forth.

What I want is to reverse it. Say if I have a table in a SQL database that stores the week numbers and I want the date of the first day of the week from the week number.
 

Imaginer

Diamond Member
Oct 15, 1999
8,076
1
0
Ok look, all I ask is an answer and not a bunch of flames. This forums is suppose to be of some help but instead I got a bunch of smart remarks. I thought posting it here would get me a answer faster since apparantly it gets ALOT of hits.

Not to mention, I dont see any problem posting such topic here.
 

DrPizza

Administrator Elite Member Goat Whisperer
Mar 5, 2001
49,601
167
111
www.slatebrookfarm.com
Geez, I don't think there's a single command that'll do that...
But instead of waiting and praying there's a simple one line solution, why not write a couple of extra lines of code to just figure it out.
(from the week number, you can easily calculate which day of the year the first day of that week was, then, from the day of the year, figure out what the date is.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Rather than bitch about it like everyone else (;)), I'll just give you a simple solution:

Public Function GetFirstDay(ByVal dWhen As Date, ByVal nDay As Integer) As Date
GetFirstDay = vbEmpty

Dim i As Integer
For i = 1 To 7
Dim dCheck As Date
dCheck = CDate(Month(dWhen) & "/" & CStr(i) & "/" & Year(dWhen))

If Weekday(dCheck) = nDay Then
GetFirstDay = dCheck
Exit Function
End If
Next
End Function

Pass in the date and the day you want.

[edit]That's VB, but just get rid of the type declarations and you can use it in VBScript.[/edit]
 

Ranger X

Lifer
Mar 18, 2000
11,218
1
0
There are software forums you can go to ask questions like these. Expert-Exchange is one of many.
 

PricklyPete

Lifer
Sep 17, 2002
14,582
162
106
I'm assuming you know what year you are in. You could easily formulate a function that used the number of days a week multiplied by the number of weeks in the db - 1. this would give you a date in the week stored in the db. From there, just

You can then use this date (using "foundDate" as variable) in the following funciton to figure out how many days you need to subtract from the date to get to the beginning of the week you want.

iDay = WeekDay(foundDate, vbSunday)

so

final Date = foundDate - iDay

 

Imaginer

Diamond Member
Oct 15, 1999
8,076
1
0
Originally posted by: PricklyPete
I'm assuming you know what year you are in. You could easily formulate a function that used the number of days a week multiplied by the number of weeks in the db - 1. this would give you a date in the week stored in the db. From there, just

You can then use this date (using "foundDate" as variable) in the following funciton to figure out how many days you need to subtract from the date to get to the beginning of the week you want.

iDay = WeekDay(foundDate, vbSunday)

so

final Date = foundDate - iDay

Thank You!!!! This may not be exactly what I am looking for but this would do NICELY!!! I was going to store data based on the week of the year but since I have this, I can make this as "week of" in my data. :) :)