Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Determine week in a month
Message
 
 
À
24/08/2017 18:52:13
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01653700
Message ID:
01653714
Vues:
33
>>>Hi,
>>>
>>>I am trying to create a function that will return the week number in a month, by any date of Monday.
>>>
>>>For example, the date "08/21/2017" (in American system) it would be 3rd week (in August). The week has to have Monday. So this month (August) the first week is the week of August 7, August 14 is week 2, August 21 is week 3, and August 28 is week 4.
>>>
>>>My code is very convoluted/complicated and I am sure there is a better way.
>>>
>>>TIA
>>
>>Ok. Here is my version:
>>
>>*--- dMonday is any Monday of the year
>>DO CASE 
>>	CASE MONTH( dMonday - 7 ) <> MONTH( dMonday )
>>		nMonthWeek = 1
>>	CASE MONTH( dMonday - 14 ) <> MONTH( dMonday )
>>		nMonthWeek = 2
>>	CASE MONTH( dMonday - 21 ) <> MONTH( dMonday )
>>		nMonthWeek = 3
>>	CASE MONTH( dMonday - 28 ) <> MONTH( dMonday )
>>		nMonthWeek = 4
>>	OTHERWISE 
>>		nMonthWeek = 1
>>ENDCASE 
>>
>>RETURN (nMonthWeek)
>>
>>
>>Do you see that it could be simpler?
>
>If your requirements specify that the given date is always a Monday, then I think this will do
>
>
>m.nMonthWeek = WEEK(m.dMonday, 3, 2) - WEEK(DATE(YEAR(m.dMonday), MONTH(m.dMonday), 1), 3, 2)
>
>
>You can generalize for any date and determine its Monday based month week, by starting to look for the Monday of the week of the date and then perform the calculation (ah! and months may have up to 5 weeks, Monday based or not).
>
>
>CLEAR
>
>LOCAL Test AS Date
>
>m.Test = {^2017-08-01}
>DO WHILE MONTH(m.Test) != 9
>	IF DOW(m.Test) = 2
>		? TEXTMERGE("<<m.Test>> = <<MondayWeek(m.Test)>>") FONT "Arial" STYLE "B"
>	ELSE
>		? TEXTMERGE("<<m.Test>> = <<MondayWeek(m.Test)>>") FONT "Arial"
>	ENDIF
>	m.Test = m.Test + 1
>ENDDO
>
>FUNCTION MondayWeek (ReferenceDate AS Date) AS Integer
>
>	LOCAL Monday AS Date
>
>	m.Monday = m.ReferenceDate - DOW(m.ReferenceDate, 2) + 1
>	
>	RETURN WEEK(m.ReferenceDate, 3, 2) - WEEK(DATE(YEAR(m.Monday), MONTH(m.Monday), 1), 3, 2)
>ENDFUNC
>
Thank you. Your one-line code (above that determines m.nMonthWeek) is very concise. I will not even check if it works because I am sure your code works 100% of the time.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform