Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date out of week 1

Status
Not open for further replies.

FILIPM

Programmer
Mar 11, 2003
40
BE
hello guys,

i'am working with vfp7 sp1.
To find a week from a date is very easy with the week() function.
Is there also an easy way to find the date of a week.
For example week 32 should give me the first date of that week namely 02/08/2004 in dmy format

Any help is usefull

Filip Merlier
Belgium
 
FILIPM

I don't believe there is any function that returns that natively. There are too many changing variables. The first day of the week is one. Depending on your situtation it could be a monday or a sunday (Take a look at SET FDOW), also check the setting for FWEEK which determine what is considered to be the first week of the year. You may need to write you own function that would look something like this (Assuming the first day of the week is a Sunday):

Code:
FUNCTION FirstDayOfTheWeek (ldDate)
local ldRetVal
DO CASE
 CASE day(ldDate) = 1
    ldRetVal = ldDate
 CASE day(lcDate) = 2  
    ldRetVal = ldDate - 1
 CASE day(lcDate) = 3  
    ldRetVal = ldDate - 2
 CASE day(lcDate) = 4  
    ldRetVal = ldDate - 3
 CASE day(lcDate) = 5  
    ldRetVal = ldDate - 4
 CASE day(lcDate) = 6  
    ldRetVal = ldDate - 5
 CASE day(lcDate) = 7  
    ldRetVal = ldDate - 6
 ENDCASE
RETURN ldRetVal



Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
To shorten all those case statements you could write it this way, but it's so short, if you don't call it often then you could just use it directly in your code rather than have a function:
Code:
FUNCTION FirstDayOfTheWeek (ldDate)
RETURN ldDate-(day(ldDate)-1)
(Also note that in mgagnon's sample code there are some typos where lcDate should be ldDate. Mike, you're still one of the best!)

dbMark
 
If I'm not mistaken he is looking for a function that wil give him the first day of the week for a given weekno.
If so then here is some code that will do just that (given first day of the week is a monday).
Please test it first before using it, if it's not 100% then at least it will give you a starting point.

Code:
FUNCTION first_dow
LPARAMETERS pnWeek, pnYear
	
	ldFirst_doy = date( iif(vartype(pnYear)=="N", pnYear, year(date())), 1, 1 )
	
	IF week(ldFirst_doy)==1
		
		ldFirst_doy = ldFirst_doy - (dow(ldFirst_doy,2)-1)
		
	ELSE
		
		ldFirst_doy = ldFirst_doy + (8-dow(ldFirst_doy,2))
		
	ENDIF 
	
	ldFirst_dow = ldFirst_doy + (pnWeek-1) * 7
	
	
	RETURN ldFirst_dow 
	
ENDFUNC


hth,

Stefan
 
one correction to my post:

IF week(ldFirst_doy)==1

should be

IF week(ldFirst_doy,2,2)==1
 
thanks stefan
this is just what I needed.
i'll give you a star for it

filip merlier
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top