Calendar woes

This is driving me crazy: calculate the number of rows in a calendar for any month in any year. For English-language calendars, in which the week begins on Sunday, no problem. But in many other countries/languages, the calendar week begins on Monday. For instance, 1 January 2023 begins on Sunday. Date.DayOfWeek returns 1 for that date and it’s easy to calculate that the calendar requires five rows but a Monday-based calendar for that month requires six rows. I have tried any number of increasingly complex if…then…else statements with no luck. Any suggestions? I feel sure there must be a simple answer but I just don’t seem able to find it. (And no, for reasons too tedious to relate here, using an off-the-shelf solution isn’t possible.)

How you calculate for Sunday calendars if you need 4, 5 or 6 rows?

If you show some code, someone will be able to help you adjust it.

Here’s the code fragment using the date d1 and returning the number of rows. MonthDays() is an array of the days in the months (there’s an adjustment for February in leap years, not shown here).

  d.Year = d1.Year
  d.Month = d1.Month
  d.Day = 1            // set the date to the first of the month
  first = d.WeekOfYear
  d.Year = d1.Year
  d.Day = MonthDays(d.Month-1)   // get number of days in the month
  last = d.WeekOfYear
  rows =  ( last - first ) + 2   // extra row is for column heads ("Sun", "Mon", etc)
 
This works just fine for weeks beginning on a Sunday.

Peter.

I’m sure there’s a more elegant approach but this works for all the months I checked. Setting DayToStartTheWeek should let you start the calendar with any day of the week.

var d1 as new date(2023, 5, 14)

var d as date
Var NumberOfDaysInMonth, DayToStartTheWeek, WeekRows, i, temp, FirstDayOfTheMonth as integer
var s, wks() as String

d = new date(d1.year, d1.Month + 1, 1)
d.day = d.day - 1
NumberOfDaysinMonth = d.day ' use XOJO dates to get the number of days in month
d.day = 1
FirstDayOfTheMonth = d.DayOfWeek

DayToStartTheWeek = 2 ' day to start the calendar row on. Sunday = 1, Monday = 2, etc.

temp = FirstDayOfTheMonth

for i = 1 to NumberOfDaysInMonth
  
  select case i
  case 1
    if temp <> DayToStartTheWeek then
      s = s + temp.ToString
    end if
  else
    s = s + temp.ToString
  end select 
  
  select case temp
  case 7
    temp = 1
  else
    temp = temp + 1
  end select
  
next

wks = s.Split(DayToStartTheWeek.ToString)

WeekRows = wks.Count
WeekRows = WeekRows + 1 ' plus one for the header

You really need to compute the number of Rows ?
(before printing the numbers ?)
If so, why ?

I think I computed where to place the first day of the month in the first week, but let the program draw the other days…by itself (skip to the next week after drawing the Sunday)…

Scott, belated thanks. I have undergone eye surgery and have dictated this with Dragon which works fine for text but not for programming. I´ll try your code once I can see what I’m doing!

Emile: I’ll try that once I’ve recovered enough eyesight (see my replay to Scott). Thanks!

I have a fixed number of rows in my Calendar. I just worked out what was the maximum number of rows that would ever be needed, based on the Month starting as late in the week as possible, and having 31 days. Then fill as many spots as required with days and blanks after that.

Call me a perfectionist if you like but I prefer not to have a blank row at the bottom if I can avoid it. I achieved this when Sunday is the first day of the week but for weeks starting on Monday it turns out to be more awkward. BTW, starting the week on Monday conforms to ISO standard 8601 which is supposedly used worldwide except in the USA, Canada and UK where the week starts on Sunday. Don’t know if other English-speaking countries such as Australia and NZ also start the week on Sunday. Having been accustomed to Sunday starting the week, a calendar using Monday as the start always looks rather odd to me, but then if the rest of the world likes it that way, who am I to argue?

Well, I know nothing of ISO standards about that. But what I will say is that I’ve never been aware that in the UK, the week supposedly starts on Sunday. For me it starts on Monday. Lessee now. Hmm, OK, a 2019 diary I have on my desk starts on Sunday. And the same for my 1967 diary. But my calendar window starts on a Monday and so the two weekend days are together.

In terms of your calendar, your blank line won’t always be blank, sometimes it will have days in it, as mine does. F’rinstance Oct 2022, where the 1st is a Saturday. So the top line has the first/second of the month, and the bottom line the 31st. This means that controls underneath (such as the Cancel/OK buttons) do not jump up and down as I scan through the months.

As far as I know there’s no definition of when a week starts in the UK. macOS Calendar app allows the user to choose any day to start the week. macOS system default is Monday, when set to UK English. Personally, I have it set to Sunday.

Right, and mine is set to Monday

I emigrated from the UK nearly 40 years ago but I seem to remember that calendars there always started the week on Sunday (often printed in red). I Googled “British calendars” and found both Sunday and Monday starts to the week, so I guess there’s no fixed standard in the UK. To me, seeing weeks starting on Monday seems somehow … odd.

Anyway, I decided to take your approach and forget about calculating the number of lines. February 2027 has two blank lines at the bottom, which seems rather inelegant, but what the hell. In case anybody’s interested, 6 lines will do for all months and years.

All of this is because DateTime.DayOfWeek returns 1 for Sunday, thus assuming that this applies everywhere.

OK. Well, nothing stops you changing it later if that seems better and you think of a satisfactory way to do it. I’ve done that with quite a few aspects of my app, as I’ve bcome more familiar with Xojo.