Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Get the Week Number by Month and Week
Message
 
 
À
25/08/2017 20:08:33
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01653756
Message ID:
01653763
Vues:
40
>>Hi,
>>
>>I have a function in my app that determines the week number in a year (1 to 53) based on the Month (1 to 12) and the week in the month (1 to 5). The Week in the Month is the week that has Monday (similar to the VFP function Week( tDate, 3, 2). My code works, which I wrote about 20 years ago. And I don't want to show it here since it could cause irrevocable damage to your health (while falling from the chair laughing).
>>
>>Therefore, I am asking, if you could create an expression or a simple function for this, I would appreciate it.
>>
>>TIA
>
>In alternative to Marco's solution, this function maps to the parameters used in the WEEK() function, so it can be used with different modes of week definitions.
>
>
>CLEAR
>
>LOCAL Test AS Date
>LOCAL Stop AS Integer
>LOCAL WeekIndex AS Integer
>
>m.Test = {^2017-01-01}
>m.Stop = YEAR(m.Test) + 1
>DO WHILE YEAR(m.Test) != m.Stop
>	* go through 5 weeks, even if the month only has 4 (in this case, the 5th will hold the same value of the first of the following month)
>	FOR m.WeekIndex = 1 TO 5
>		? TEXTMERGE("<<YEAR(m.Test)>>/<<MONTH(m.Test)>>, <<m.WeekIndex>> = <<MonthWeekToYearWeek(m.Test, m.WeekIndex)>>")
>	ENDFOR
>	m.Test = GOMONTH(m.Test, 1)
>ENDDO
>
>#DEFINE DEFAULT_FIRSTWEEK			3
>#DEFINE DEFAULT_FIRSTDAYOFWEEK	2
>
>FUNCTION MonthWeekToYearWeek (ReferenceDate AS Date, MonthWeek AS Integer, FirstWeek AS Integer, FirstDayOfWeek AS Integer) AS Integer
>
>	LOCAL FirstDayOfReferenceWeek AS Date
>
>	* set your defaults
>	IF PCOUNT() < 3
>		m.FirstWeek = DEFAULT_FIRSTWEEK
>	ENDIF
>	IF PCOUNT() < 4
>		m.FirstDayOfWeek = DEFAULT_FIRSTDAYOFWEEK
>	ENDIF
>
>	* calculate the first day of the reference week in the reference month
>
>	* start at the beginning of the year
>	m.FirstDayOfReferenceWeek = DATE(YEAR(m.ReferenceDate), 1, 1)
>
>	* get the first day of the year that it is in the first week of the year
>	* for a single week mode - the default, for instance - this could be make simpler,
>	*   but this method adjusts to the different settings defined by the WEEK() parameters
>	DO WHILE WEEK(m.FirstDayOfReferenceWeek, m.FirstWeek, m.FirstDayOfWeek) != 1
>		m.FirstDayOfReferenceWeek = m.FirstDayOfReferenceWeek + 1
>	ENDDO
>
>	* now jump closer to the equivalent day in the reference month (in steps of 7 * 4 days - the minimum month length)
>	m.FirstDayOfReferenceWeek = m.FirstDayOfReferenceWeek + (MONTH(m.ReferenceDate) - 1) * 28
>	* eventually, we'll have to step further, once or twice
>	DO WHILE MONTH(m.FirstDayOfReferenceWeek) < MONTH(m.ReferenceDate)
>		m.FirstDayOfReferenceWeek = m.FirstDayOfReferenceWeek + 7
>	ENDDO
>
>	* we have by now the first day of the first week of the month, we can step to the the required week in the month
>	m.FirstDayOfReferenceWeek = m.FirstDayOfReferenceWeek + (m.MonthWeek - 1) * 7
>
>	* return the corresponding year week
>	RETURN WEEK(m.FirstDayOfReferenceWeek, m.FirstWeek, m.FirstDayOfWeek)
>
>ENDFUNC
>
Antonio,
Thank you very much.
"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