Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program to check date for leap year

Status
Not open for further replies.

bingono1

Programmer
Joined
Dec 19, 2003
Messages
3
Location
KE

How can I write a program that will assist me to know the leap year dates as well as determine when Easter Holiday are around the corner
 
I don't know how Easter is calulated, but I think it often falls on the Sunday after Passover. I do know that if Passover happens to fall on a Sunday, then Easter will usually be a different Sunday.

Determining if a year is a leap year:
Code:
? leapyear(2003)
? leapyear("2003")

FUNCTION leapyear
PARAMETERS xYear
PRIVATE cDate
* xYear can be either Numeric or Character
cDate="02/29/"+IIF(TYPE("xYear")="N",STR(xYear,4),xYear)
RETURN cDate==DTOC(CTOD(cDate))
dbMark
 
Here's a 2.x compatible version of a FAQ written by Mike Lewis in the VFP forum for calculating easter. Call it by using the year as a numeric:

?easter(2004)

Code:
FUNCTION Easter
* Calculates the date of Easter for a given year. 
* Accepts years in the range 100 to 9999. However, 
* for years before the adoption of the Gregorian
* Calendar (1582 - 1587 in much of Europe, 1752 in 
* Britain and North America) the calculation 
* is inappropriate.

PARAMETERS tnYear

IF EMPTY(tnYear) OR NOT BETWEEN(tnYear,100,9999)
  ?"Invalid year for Easter calculation"
ENDIF 

*LOCAL lnCentury, lnG, lnI, lnK, lnJ, lnL
*LOCAL lnEasterDay, lnEasterMonth

lnCentury = INT(tnYear/100)
lnG = MOD(tnYear, 19)
lnK = INT((lnCentury - 17)/25)
lnI = MOD((lnCentury - INT(lnCentury/4) - ;
  INT((lnCentury - lnK)/3) + 19*lnG + 15), 30)
lnI = lnI - INT(lnI/28) * ((1 - INT(lnI/28) * ;
  (INT(29/(lnI+1)) * (INT((21-lnG)/11)))))
lnJ = MOD((tnYear + INT(tnYear/4) + lnI + 2 - ; 
  lnCentury + INT(lnCentury/4)), 7)
lnL = lnI - lnJ
lnEasterMonth = 3 + INT((lnL + 40)/44)
lnEasterDay = lnL + 28 - 31*INT(lnEasterMonth/4)

return alltrim(str(lnEasterMonth)) + '/' + ;
       alltrim(str(lnEasterDay)) + '/' + ;
       alltrim(str(tnYear))


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Dave,

That code generate wrong dates between years 100 to 1281, and between 4109 to 9999. Here's a more accurate one:
Code:
Function Easter
   Para y
   FirstDig = Int(y / 100)
   Remain19 = Mod(y, 19)
   temp = Int((FirstDig - 15) / 2) + 202 - 11 * Remain19
   Do case
     Case inlist(FirstDig, 21, 24, 25, 34, 35, 38) or ;
          between(FirstDig, 27, 32)
       temp = temp - 1
     Case inlist(FirstDig, 33, 36, 37, 39, 40)
       temp = temp - 2
   Endc
   temp = Mod(temp, 30)
   tA = temp + 21
   If temp = 29 
     tA = tA - 1
   Endi
   If (temp = 28 And Remain19 > 10) 
     tA = tA - 1
   Endi
   tB =  Mod((tA - 19), 7)
   tC = Mod((40 - FirstDig), 4)
   If tC = 3 
     tC = tC + 1
   Endi
   If tC > 1 
     tC = tC + 1
   Endi
   temp = Mod(y,100)
   tD =  Mod((temp + int(temp / 4)), 7)
   tE = (Mod((20 - tB - tC - tD), 7)) + 1
   d = tA + tE
   If d > 31 
      d = d - 31
      m = 4
   Else
      m = 3
   Endi
Retu Strt(Str(m,2)+"/"+Str(d,2)+"/"+Allt(Str(y,4))," ","0")
This code is based on a VB code found in this site:

 
I just realized that the user may have SET CENTURY OFF, so this modified code should be more robust to handle either setting as well as most settings for SET DATE order

Determining if a year is a leap year:
Code:
? leapyear(2003)
? leapyear("2003")

FUNCTION leapyear
PARAMETERS xYear
* xYear can be either Numeric or Character
PRIVATE cYear, cDate, lMatch
cYear=IIF(TYPE("xYear")="N",STR(xYear,4),xYear)
cDate=IIF(LEFT(SET("DATE"),3)$"AME/USA/MDY","02/29/"+cYear,;
      IIF(LEFT(SET("DATE"),3)$"JAP/TAI/YMD",cYear+"/02/29",;
      "29/02/"+cYear)
RETURN LEFT(DTOC(CTOD(cDate)),2)<>&quot;  &quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top