Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Number of Mondays in a month
Message
From
20/10/2016 02:42:22
 
 
To
19/10/2016 20:31:42
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01642129
Message ID:
01642134
Views:
71
>The application has to determine how many occurrences of a given day of the week occurred in a given calendar month.
>
>For example- How many Mondays (or any other specified day of the week) were there in February, 2016?
>I can see how to get the answer by looping through all the days in the month in question, but is there a slicker way?
* Sunday = 1, etc...

CLEAR

LOCAL Loop AS Integer

FOR m.Loop = 1 TO 7

	? m.Loop, WeekDaysInMonth(m.Loop,2016,10)
	
ENDFOR


FUNCTION WeekDaysInMonth (WeekDay AS Integer, CalendarYear AS Integer, CalendarMonth AS Integer) AS Integer

	LOCAL StartDay AS Date
	LOCAL FirstWeekDay AS Integer
	LOCAL ExtraDaysInMonth AS Date
	LOCAL Result AS Integer
	
	m.Result = 4
	
	m.StartDay = DATE(m.CalendarYear, m.CalendarMonth, 1)
	m.FirstWeekDay = DOW(m.StartDay)
	m.ExtraDaysInMonth = (GOMONTH(m.StartDay,1) - m.StartDay) - 28
	
	IF m.ExtraDaysInMonth > IIF(m.WeekDay < m.FirstWeekDay,m.WeekDay + 7 - m.FirstWeekDay,m.WeekDay - m.FirstWeekDay)
		m.Result = m.Result + 1
	ENDIF
	
	RETURN m.Result

ENDFUNC
----------------------------------
António Tavares Lopes
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform