B4J Code Snippet B4X - cHolidays - Determine when a certain holiday is

SubName: cHolidays

Description:

I came across some excel formula's (https://www.tek-tips.com/faqs.cfm?fid=7549) for determining when a holiday was and on what day / date.
Thought it was an interesting way of doing it so I converted the code to something I could use in B4A and B4J

Some of cHolidays that you can check for are not real holidays "Black Friday" for one.

WhenIs_NewYearsDay
WhenIs_NewYearsEve

WhenIs_MartinLutherKingDay
WhenIs_PresidentsDay
WhenIs_WashingtonsBirthday

WhenIs_ValentinOsDay
WhenIs_SaintPattysDay
WhenIs_ArborDay
WhenIs_CincoDeMayo

WhenIs_MothersDay
WhenIs_FathersDay

WhenIs_MemorialDay
WhenIs_IndependenceDay
WhenIs_LaborDay
WhenIs_ColumbusDay
WhenIs_ElectionDay
WhenIs_VeteransDay

WhenIs_ThanksgivingDay
WhenIs_BlackFriday

WhenIs_ChristmasDay
WhenIs_Easter

Code: Is attached with B4J test program

Dependencies: None

Tags: cHolidays, NewYearsDay, NewYearsEve, MartinLutherKingDay, PresidentsDay, WashingtonsBirthday, ValentinOsDay, SaintPattysDay, ArborDay, MothersDay, FathersDay
Tags: MemorialDay, IndependenceDay, LaborDay, ColumbusDay, ElectionDay, VetransDay, ThanksgivingDay, BlackFriday, ChristmasDay, Easter, Cinco de Mayo

Version: 1.4 - Added Easter code provided
1.5 - Added Cinco de Mayo
1.6 - Fixed typeo

Enjoy

BobVal
 

Attachments

  • Holidays_B4X.zip
    4.2 KB · Views: 313
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
I couldn't resist to add Easter.

B4X:
#Region WhenIs_EasterDay
Public    Sub    WhenIs_EasterDay(ForYear As Int) As THolidayIs

            Dim HolidayIs As THolidayIs
            HolidayIs.Initialize
            HolidayIs.IsValid    = True
                        
            'This Haskell function computes the date of Easter Sunday for a given year, as used by the United States Naval Observatory
            
            Dim C As Long
            Dim N As Long
            Dim K As Long
            Dim I As Long
            Dim J As Long
            Dim L As Long
            Dim M As Long
            Dim D As Long
            
            C = Floor(ForYear / 100)
            N = ForYear - 19 * Floor(ForYear / 19)
            K = Floor((C - 17) / 25)
            I = C - Floor(C / 4) - Floor((C - K) / 3) + 19 * N + 15
            I = I - 30 * Floor(I / 30)
            I = I - Floor(I / 28) * (1 - Floor(I / 28) * Floor(29 / (I + 1)) * Floor((21 - N) / 11))
            J = ForYear + Floor(ForYear / 4) + I + 2 - C + Floor(C / 4)
            J = J - 7 * Floor(J / 7)
            L = I - J
            M = 3 + Floor((L + 40) / 44)
            D = L + 28 - 31 * Floor(M / 4)

            Dim EasterThisYear As Long = DateTime.DateParse($"${NumberFormat2(M, 2, 0, 0, False)}/${NumberFormat2(D, 2, 0, 0, False)}/${NumberFormat2(ForYear, 4, 0, 0, False)}"$)

                    
            HolidayIs.Month        = DateTime.GetMonth(EasterThisYear)
            HolidayIs.Day        = DateTime.GetDayOfMonth(EasterThisYear)
            HolidayIs.Year        = DateTime.GetYear(EasterThisYear)
            HolidayIs.DayOfWeek    = DateTime.GetDayOfWeek(EasterThisYear)

            Log("         Easter Day Is " &NumberFormat2(HolidayIs.Month, 2, 0, 0, False) &"/" &NumberFormat2(HolidayIs.Day, 2, 0, 0, False) &"/" &NumberFormat2(HolidayIs.Year, 4, 0, 0, False) &"  -  " &DEFINE_DaysOfWeek(HolidayIs.DayOfWeek))
            
            Return HolidayIs
End Sub
#end region
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Since this is a multicultural forum I will post to chit chat to request contributions from others. There must be hundreds/thousands of holidays.
 

derez

Expert
Licensed User
Longtime User
I have a working application to check if a date is a Hebrew/Israeli Holiday/Event. It uses the Dateconvert library https://www.b4x.com/android/forum/threads/dateconvert-library.16770/post-95800. They usually move based on the Day Of Week. If anyone is interested I'll publish it. The following is just the names of the Holidays:
B4X:
Hag_name(0) = "ראש השנה"
Hag_name(1) = "יום הכיפורים"
Hag_name(2) = "סוכות"
Hag_name(3) = "חול המועד סוכות"
Hag_name(4) = "סוכות שני הושענא רבא"
Hag_name(5) = "שמחת תורה"
Hag_name(6) = "חנוכה"
Hag_name(7) = "צום עשרה בטבת"
Hag_name(8) = "ראש השנה לאילנות"
Hag_name(9) = "תענית אסתר"
Hag_name(10) = "פורים"
Hag_name(11) = "שושן פורים"
Hag_name(12) = "פסח"
Hag_name(13) = "חול המועד פסח"
Hag_name(14) = "פסח שני"
Hag_name(15) = "יום הזכרון לשואה ולגבורה"
Hag_name(16) = ""
Hag_name(17) = "יום העצמאות"
Hag_name(18) = "יום הזכרון לחללי צה''ל"
Hag_name(19) = "ל''ג בעומר"
Hag_name(20) = "שבועות"
Hag_name(21) = "צום י''ז בתמוז"
Hag_name(22) = "תשעה באב"
Hag_name(23) = "חג האהבה"
Hag_name(24) = "צום גדליה"
Hag_name(25) = "יום הזכרון ליצחק רבין"
Hag_name(26) = "יום ירושלים"
 

rabbitBUSH

Well-Known Member
Licensed User
I couldn't resist to add Easter.

Does this take care of Easter following a lunar cycle (not sure but I think it does)

And
Leap year:
C = (c/4)

If I recall from ComScio_O exercises every 400th year is NOT a leap year... Not sure if your code accommodates for that.?
 

emexes

Expert
Licensed User
WhenIs_ValentinOsDay ?

Is that same as Valentine's Day ? Or your wedding anniversary?


WhenIs_VetransDay ?

Is that Veterans Day ? I feel like that is same as Rememberance Day here (Australia, and rest of British Commonwealth, maybe even Canada too).


If you are looking to standardize the naming of the holidays, perhaps use Wikipedia.

Eg, use SaintPatricksDay instead of SaintPaddysDay.

Or somehow link with www.timeanddate.com, they're pretty good.
 
Last edited:

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Fixed Veterans misspelling. WOW you would think as a vet I could spell it.

As I said in the 1st post. I came across these Excel formulas for dates and thought I was interesting. Wasn't trying to do anything else but share.

As for ValentinOsDay. My last name is Valentino and my Mom always called it ValentinO's day (Always with the big O).
Later in life I moved from NY to Virginia and actually live the town of Valentines You have any problems with that it's just a 1 keystroke fix.

As for SaintPattysDay. My wife's name is Patricia or Patty and after 43+ years of living with me she should be sainted (is that a word).

AS for holidays, I remember 20 years ago when I was working that to me a Holiday was when I got the day off from work. So about 75% of these don't qualify, but for my LED Strip light display they do.

Happy New Year and Enjoy
 

emexes

Expert
Licensed User
As for ValentinOsDay. My last name is Valentino and my Mom always called it ValentinO's day (Always with the big O).
I like your rationale better. Now I'm going to be thinking of you every February 14th :-/

Did you ever find out, why the capital O?

As for SaintPattysDay. My wife's name is Patricia or Patty and after 43+ years of living with me she should be sainted
Lol. And if that is also your marriage anniversary: perfect.
 
Last edited:

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
Cool.

Here is a snippet for Orthodox Easter calculation (results checked until 2070):

B4X:
#Region WhenIs_OrthodoxEasterDay
Public    Sub WhenIs_OrthodoxEasterDay(ForYear As Int) As THolidayIs

    Dim HolidayIs As THolidayIs
    HolidayIs.Initialize
    HolidayIs.IsValid    = True
                       
    Dim startdate As Long = DateTime.DateParse("04/01/" & ForYear)
       
    Dim E As Int =((11*((ForYear-2) Mod 19)) Mod 30)
       
    Dim basedate As Long
    If E>23 Then
        basedate=DateTime.Add(startdate,0,0,56-E)  
    Else
        basedate=DateTime.Add(startdate,0,0,25-E)      
    End If
   
    Dim dow As Int =DateTime.GetDayOfWeek(basedate)
   
    Dim OrthEaster As Long
   
    If basedate > DateTime.DateParse("05/01/" & ForYear) And dow=1 Then
        OrthEaster= basedate
    Else
        OrthEaster =DateTime.Add(basedate,0,0,8-dow)
    End If
                   
    HolidayIs.Month        = DateTime.GetMonth(OrthEaster)
    HolidayIs.Day        = DateTime.GetDayOfMonth(OrthEaster)
    HolidayIs.Year        = DateTime.GetYear(OrthEaster)
    HolidayIs.DayOfWeek    = DateTime.GetDayOfWeek(OrthEaster)

    Log("            Orthodox Easter Day Is " &NumberFormat2(HolidayIs.Month, 2, 0, 0, False) &"/" &NumberFormat2(HolidayIs.Day, 2, 0, 0, False) &"/" &NumberFormat2(HolidayIs.Year, 4, 0, 0, False) &"  -  " &DEFINE_DaysOfWeek(HolidayIs.DayOfWeek))
           
    Return HolidayIs
End Sub
#end region
 
Top