Calendar-grid helpers

Building blocks for rendering a month view, like a typical calendar UI.

func FirstWeekdayOfMonth(year, month int) (time.Weekday, error)

The day of the week that day 1 of the given month falls on.

func WeeksInMonth(year, month int) (int, error)

The number of calendar-grid rows needed to display the month, with weeks running Sunday through Saturday — the same row count MonthCalendar returns.

func MonthCalendar(year, month int) ([][]*Date, error)

A week-by-week grid of the month. Each week is exactly 7 cells (Sunday through Saturday); a nil cell means no day of this month falls in that slot — padding at the start of the first week and/or the end of the last week. Every date from day 1 to the month's last day appears exactly once, in order.

weeks, _ := bs.MonthCalendar(2083, 6) // [][]*bs.Date, Sunday-first, nil-padded
 
for _, week := range weeks {
	for _, day := range week {
		if day == nil {
			fmt.Print("   ")
		} else {
			fmt.Printf("%2d ", day.Day)
		}
	}
	fmt.Println()
}
 
// Su Mo Tu We Th Fr Sa
//              1  2  3
//  4  5  6  7  8  9 10
// 11 12 13 14 15 16 17
// 18 19 20 21 22 23 24
// 25 26 27 28 29 30 31

Try it

This grid is rendered live, in the browser, by calling the actual MonthCalendar function (compiled to WebAssembly) — not a JavaScript reimplementation.

Loading…