Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How many Sundays in a month?
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00487391
Message ID:
00487438
Vues:
9
Hi Erik,

Unfortunately that returns a 5 for this month, when, in fact, there are only 4 sundays.

Here's one that seems to work:
* Program....: SUNINMONTH.PRG
* Version....: 1.0
* Author.....: Andrew Coates
* Date.......: March 22, 2001
* Notice.....: Copyright © 2001 Civil Solutions, All Rights Reserved.
* Compiler...: Visual FoxPro 06.00.8961.00 for Windows
* Abstract...: Establishes the number of Sundays in a given month
*  optionally pass the month (1-12) (assumes the current month) and the 
*  year (100 - 9999) (assumes the current year)
* Changes....:

lparameters tnMonth, tnYear

local lnMonth, lnYear

if vartype(tnMonth) # 'N' or ! between(tnMonth, 1, 12)
	lnMonth = month(date())
else
	lnMonth = int(tnMonth)
endif

* date() takes year between 100 and 9999
if vartype(tnYear) # 'N' or ! between(tnYear, 100, 9999)
	lnYear = year(date())
else
	lnYear = int(tnYear)
endif

* get the first day in the month
local lnFirstDay, ldFirstDayDate
ldFirstDayDate = date(lnYear, lnMonth, 1)

* what day is the first day
* note that the dow() function uses Sunday = 1 unless a second parameter is passed, 
* regardless of the regional settings
* we'll use sunday = day 7 (Monday = 1) for this calc as it makes things easier
lnFirstDay = dow(ldFirstDayDate, 2)

* work out the number of days in the month (and therefore the number of extra days over 4 weeks)
lnDaysInMonth = gomonth(ldFirstDayDate, 1) - ldFirstDayDate
lnExtraDays = lnDaysInMonth - 28

* For there to be an extra sunday, lnFirstDay must fall on a Sunday (for 29 day month),
* a Saturday or a Sunday (for 30 day month) or on a
* Friday, Saturday or Sunday (for a 31 day month)
* so the first day has to be be between (7 - lnExtraDays + 1) and 7
lnSundays = 4 + iif(between(lnFirstDay, (7 - lnExtraDays + 1),7), 1, 0)

return lnSundays
Cheers,

Andrew
>Try this:
>
>
>FUNCTION countSundays
>LPARAMETERS pnYear, pnMonth
>LOCAL ldDate1, ldDate2
>ldDate1 = date(pnYear, pnMonth, 1)
>ldDate2 = gomonth(ldDate1, 1)
>
>return int((d2-d1+dow(d1)) / 7)
>ENDFUNC
>
>
>
Hi All, What would be the best approach to find out how many Sundays are with in a month?
>>
>>would I have to iterate each day, and count how many dow()=1 and count them between the first of the month and last off the month?
>

>
>HTH,


If we were to introduce Visual FoxBase+, would we be able to work from the dotNet Prompt?


From Top 22 Developer Responses to defects in Software
2. "It’s not a bug, it’s a feature."
1. "I thought I fixed that."


All my FoxTalk and other articles are available on my web site.


Unless specifically identified otherwise, anthing posted here is purely my opinion and may or may not reflect the policies or practices of Microsoft.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform