Livecode Function That Returns Number Of Days In A Month
(Includes Leap Year Algorithm And Method To Determine Offset For The First Day Of The Month)

Screenshot of simple calendar I wrote to test code.

⬅ Back To LiveCode Main Page


Below is a function I tried and worked in a simple calendar I threw together one afternoon. The function is broken up into three sections.

  1. Building a line delimited variable named dayCount with the number of days for each month. It will be used by the function to return the proper number of days except for February. Line 2 or February is only a line spacer as the number of days for it will be determined by a leap year algorithm.

  2. Leap year algorithm. There are several steps to properly determine a leap year and this algorithm does that. For more information on leap years and why multiple steps are required, see my PDF document titled Why We Have Leap Years

  3. Livecode method to calculate the offset for the first day of the month. As you can see from the above screenshot, the offset for October 2021 is 6 or Friday.


function getMonthLayout mo yr
---------- Begin loading number of days of each month into variable 'dayCount' ----------
put 31 & cr into dayCount # Days for January
put 28/29 & cr after dayCount #Used only as a spacer. Will use a leap year algorithm for February
put 31 & cr after dayCount # Days for March
put 30 & cr after dayCount # Days for April
put 31 & cr after dayCount # Days for May
put 30 & cr after dayCount # Days for June
put 31 & cr after dayCount # Days for July
put 31 & cr after dayCount # Days for August
put 30 & cr after dayCount # Days for September
put 31 & cr after dayCount # Days for October
put 30 & cr after dayCount # Days for November
put 31 after dayCount # Days for December
---------- End loading number of days of each month into variable 'dayCount' ----------

---------- Begin Leap Year algorithm ----------
if mo is not 2 then # If the month is not February...
put line mo of dayCount into numOfDays #...get the number of days of the month from varable 'dayCount'
else #...but if it is February then execute the following code.
if yr mod 4 is 0 then # If the year divided by 4 has a remainder of zero, then run next test.
if yr mod 100 is 0 then # If the year divided by 100 has a remainder of zero, then run next test
if year mod 400 is 0 then # If year divided by 400 has a remainder of zero...
put 29 into NumOfDays # then it is a leap year but...
else # if the year divided by 400 has a remainder greater than zero then...
put 28 into NumOfDays #...it is not a leap year.
end if
else
# If year divided by 100 has a remainder greater than zero...
put 29 into NumOfDays #...then it is a leap year.
end if
else
# If year divided by 4 has a remainder greater than zero...
put 28 into NumOfDays #...then it is not a leap year.
end if
end
if
---------- End Leap Year algorithm ----------

---------- Begin algorithm to calculate 1st day offset of the month ----------
set itemdelimiter to comma # Set item delimiter to comma. Will be used to parse the comma-delimited date MoDyYr variable.
put mo & "/1/" & yr into MoDyYr # Build a date with month, first day, and year and place it into the variable 'MoDyYr'
convert MoDyYr to dateitems # Convert the format of MoDyYr to dateitems, a comma-delimited date format.
put last item in MoDyYr into calOffset # Grab the last item in MoDyYr which is a numeral 1 to 7 for the day of week. This will be the...
#...1st day of the month.
---------- End algorithm to calculate 1st day offset of the month ----------

return numOfDays, calOffset # Returns a comma delimited variable. Item 1 = Number of Days in Month & Item 2 = First Day of Month Offset

end getMonthLayout

Below is how I called the function from within the Option Menu Buttons for the month and year. Everytime the month or year was changed, it would called the function to update the calendar.

on menuPick pItemName
put word 2 of selectedline of button "optSelMonth" into mo #Get the month number from line number of option menu button for months
put label of button "optSelYear" into yr #Put year number from label of the option menu button for years
get getMonthLayout (mo,yr) # Call function to get a comma delimited variable with the number of days and the first day of month offset,
loadCalendar it # Call subroutine with the information from loadDays function. This subroutine will properly layout the days in the calendar.
checkToday mo, yr #Check if the calendar has today's day and if so, move image of red circle (that I drew in the GIMP) over the day and set it to be visible.
end menuPick