Android Question [Solved] Calculate Easter Day

asales

Well-Known Member
Licensed User
I found these functions (in VB) that calculates easter day, but I could not convert to B4A (I tried but don't worked):
http://www.cpearson.com/excel/easter.aspx
http://www.codeproject.com/Tips/6630/Computing-Easter-Date-VB-Function

I tried to convert this function made in Delphi...

B4X:
function GetGoldenNumber (const Year: Word): Integer;
begin
   Result := Year mod 19 + 1;
End;

function GetEasterSunday (const Year: Word): TDateTime;
var
   C, I, J, H, G, L: Integer;
   D, M: Word;
begin
   G := GetGoldenNumber (Year) - 1;
   C := Year div 100;
   H := (C - C div 4 - (8 * C + 13) div 25 + 19 * G + 15) mod 30;
   I := H - (H div 28) * (1 - (H div 28) * (29 div (H + 1))*((21 - G) div 11));
   J := (Year + Year div 4 + I + 2 - C + C div 4) mod 7;

   L := I - J;
   M := 3 + (L + 40) div 44;
   D := L + 28 - 31 * (M div 4);
   Result := EncodeDate (Year, M, D);
End;

to this:

B4X:
Sub GetGoldenNumber (Year As Long) As Int
   Return Year Mod 19 + 1
End Sub

Sub GetEasterSunday (Year As Long) As Long
       Dim C, I, J, H, G, L As Int
       Dim D, M As Long

       G = GetGoldenNumber(Year) - 1
       C = Year / 100
       H = (C - C / 4 - (8 * C + 13) / 25 + 19 * G + 15) Mod 30
       I = H - (H / 28) * (1 - (H / 28) * (29 / (H + 1))*((21 - G) / 11))
       J = (Year + Year / 4 + I + 2 - C + C / 4) Mod 7

       L = I - J
       M = 3 + (L + 40) / 44
       D = L + 28 - 31 * (M / 4)

    Return DateTime.DateParse(M & "/" & D & "/" & Year)
End Sub

Label1.Text = DateTime.Date(GetEasterSunday(EditText1.Text))

but did not work too.

The easter sunday in 2015 is April 5 and in 2016 is March 27.

I appreciate any help to make this function works.
 

DonManfred

Expert
Licensed User
NO B4A-Code(!)... Just the info about the calculation to check it (found here)
B4X:
  a = J Mod 19
  b = J Mod 4
  c = J Mod 7
  d = (19 * a + 24) Mod 30
  e = (2 * b + 4 * c + 6 * d + 5) Mod 7
  OT = 22 + d + e
  OM = 3
  If OT > 31 Then
  OT = d + e - 9
  OM = 4
  End If
  If OT = 26 And OM = 4 Then
  OT = 19
  End If
  If OT = 25 And OM = 4 And d = 28 And e = 6 And a > 10 Then
  OT = 18
  End If

Where
J= Number of the year
OM= Month of the Easter Sunday
OT= Day of the Easter Sunday in this month

Original from: Peter Wünsche --- peter.wuensche@ktheo.uni-bamberg.de --- 25.07.95
 
Upvote 0

sorex

Expert
Licensed User
remove that goldyear sub and use this instead

B4X:
Sub GetEasterSunday (Year As Long) As String
       Dim C, I, J, H, G, K,L,N As Int
       Dim D, M As Long
       G = Year Mod 19
       C = Year/100
       H = ((C - (C / 4) - (8 * C + 13) / 25) + 19 * G + 15) Mod 30
       I = H - (H / 28) * (1 - (H / 28) * (29 / (H + 1))*((21 - G) / 11))
       J = (Year + (Year / 4) + I + 2 - C + (C / 4)) Mod 7
       L = I - J
       M = 3 + ((L + 40) / 44)
       D = L + 28 - 31 * Floor(M / 4)             
    Return (M & "/" & D & "/" & Year)
End Sub

Log( GetEasterSunday(2015) )
Log( GetEasterSunday(2016) )


outputs

** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
4/5/2015
3/27/2016
** Activity (main) Resume **
 
Upvote 0

sorex

Expert
Licensed User
it did in the original but I didn't see the point of it as you want to display it as text anyway, not?
 
Upvote 0

sorex

Expert
Licensed User
oh you mean to combine it with dateformat so that you get dd/mm/yyyy for us euros instead?
 
Upvote 0

NJDude

Expert
Licensed User
A slightly enhanced version of Sorex's code:
B4X:
'Format: L = Long date
Sub GetEasterSunday(Year As Long, Format As String) As String

    Private C, I, J, H, G, L As Int
    Private D, M As Long
    Private Month() As String
 
    Month = Array As String("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

    G = Year Mod 19
    C = Year/100
    H = ((C - (C / 4) - (8 * C + 13) / 25) + 19 * G + 15) Mod 30
    I = H - (H / 28) * (1 - (H / 28) * (29 / (H + 1))*((21 - G) / 11))
    J = (Year + (Year / 4) + I + 2 - C + (C / 4)) Mod 7
    L = I - J
    M = 3 + ((L + 40) / 44)
    D = L + 28 - 31 * Floor(M / 4)             

    If Format.ToUpperCase = "L" Then
				
       Return (Month(M - 1) & " " & D & ", " & Year)
							
    Else
				
       Return (M & "/" & D & "/" & Year)
							
    End If

End Sub
 
Upvote 0

Rick Harris

Well-Known Member
Licensed User
Here is a complete B4A project that not only calculates first Easter day, but many more festive days with an annually changing date:
B4X:
#Region  Project Attributes
    #ApplicationLabel: DSH electronics Festive Days calculator
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    GetFestiveDay("First Easter Day", 2015,0)    'First Easter Day
    GetFestiveDay("Second Easter Day",2015,1)    'Second Easter Day
    GetFestiveDay("Holy Saturday",2015,-1)       'Holy Saturday
    GetFestiveDay("Good Friday",2015,-2)         'Good Friday
    GetFestiveDay("White Thursday",2015,-3)      'White Thursday
    GetFestiveDay("Ascension Day",2015,39)       'Ascension Day
    GetFestiveDay("Whitsun SunDay",2015,49)      'Whitsun SunDay
    GetFestiveDay("Whitsun Monday",2015,50)      'Whitsun Monday
    GetFestiveDay("Trinity day",2015,56)         'Trinity day
    GetFestiveDay("Sacrements day",2015,59)      'Sacrements day
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub GetFestiveDay (Descript As String, Year As Long, Day As Long) As String
    Dim C, I, J, H, G, L As Int
    Dim D, M As Long
    Dim D1, M1 As String
    Dim WDay As Int
    Dim ETicks1 As Long
    Dim EDate1 As String
    Dim WeekDay(8) As String
    WeekDay(1) = "Sun. "
    WeekDay(2) = "Mon. "
    WeekDay(3) = "Tue. "
    WeekDay(4) = "Wed. "
    WeekDay(5) = "Thu. "
    WeekDay(6) = "Fri. "
    WeekDay(7) = "Sat. "

    G = Year Mod 19
    C = Year/100
    H = ((C - (C / 4) - (8 * C + 13) / 25) + 19 * G + 15) Mod 30
    I = H - (H / 28) * (1 - (H / 28) * (29 / (H + 1))*((21 - G) / 11))
    J = (Year + (Year / 4) + I + 2 - C + (C / 4)) Mod 7
    L = I - J
    M = 3 + ((L + 40) / 44)
    D = L + 28 - 31 * Floor(M / 4)
    M1=M
    D1=D
    If M1.Length = 1 Then M1 = "0" & M1
    If D1.Length = 1 Then D1 = "0" & D1
    DateTime.DateFormat = "dd/MM/yyyy"
    ETicks1 = DateTime.DateParse(D1 & "/" & M1 & "/" & Year)
    EDate1 = DateTime.Date(ETicks1+(Day * 86400000))
    WDay = DateTime.GetDayOfWeek(ETicks1+(Day * 86400000))
    Log(Descript & "= " & WeekDay(WDay) & EDate1)
End Sub
 
Upvote 0
Top