Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Determine week in a month
Message
 
 
À
25/08/2017 06:58:44
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01653700
Message ID:
01653747
Vues:
44
>>>>>>>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?
>>>>>
>>>>>
>>>>>Well, let me beat the horse Dragan already targeted from another angle:
>>>>>there are months with more than 28 days, which your code will press into 4 weeks. Some weeks of those I'd prefer not to work but count as holiday weeks as they have more than 7 days ;-)
>>>>
>>>>You are lucky that you only have to work 35 hours a week :). In American, we work 24/7 :).
>>>
>>>Then why create a function to create 24/8, 24/9 and 24/10 weeks as well ?
>>
>>Sorry I miss your point (if you are not joking). I could not find a case where my function (above) would return any other number but 1,2,3, or 4. Where do you get 8,9,10?
>
>During the in-between-posts you probably realized, that your original function will return 1 for dates like ^2017.10.30 as well as the intended ^2017.10.02, thereby creating a Litvakian first week of October with 2 Mondays, so 24/8 at least ;-)))
>
>IAC I would try to steer away from "week" for this operational definition for the function or its results, as this will automatically fit non-monday dates like ^2017.11.01 into the 5th week of October as well.
>
>Ask if any Monday given as parameter is 1st, 2nd, 3rd, 4th or 5th as result of function (and better add guarding check inside the fuction than depend on programming logic depending on calling code). Then ^2017.11.01 can be said to fall in the week starting on 5th monday of October without any "Huh?" moments.

As I mentioned at the top of the thread, that the Date given to this function is always and only a Monday. So the function will never see the date of ^2017.11.01, since it is not a Monday.
Therefore, so far, the formula/code given by Dragan works.
However, I may change the entire logic of this routine. Simply based on the Business, not on code.
Thank you.
"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