How to print every month in a specified year that contains five full weekends (Fri, Sat, Sun) in Go

1 Answer

0 votes
// A month has five full weekends when it has 31 days,
// and the 1st day of the month is a Friday.

package main

import (
    "fmt"
    "time"
)

// ------------------------------------------------------------
// Month names as a single reusable constant
// ------------------------------------------------------------
var monthNames = [12]string{
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December",
}

// ------------------------------------------------------------
// Function: returns true if a month has 5 full weekends
// ------------------------------------------------------------
func hasFiveFullWeekends(year int, month time.Month) bool {
    firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)

    daysInMonth := firstDay.AddDate(0, 1, -1).Day()
    weekday := firstDay.Weekday() // Sunday=0 ... Friday=5 ... Saturday=6

    return daysInMonth == 31 && weekday == time.Friday
}

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
func main() {
    yValue := 2026

    for m := time.January; m <= time.December; m++ {
        if hasFiveFullWeekends(yValue, m) {
            fmt.Printf("%s %d has five full weekends.\n",
                monthNames[m-1], yValue)
        }
    }
}


/*
run:

May 2026 has five full weekends.

*/

 



answered May 26 by avibootz

Related questions

...