Overview

In honor of Leap day, I wrote a small Go program that checks if the current date is a leap day. It uses constants and functions from the time package.

Code

Here is the full code for this program. It starts out by saving the current local time and printing the date to the console with the time.UnixDate format. The local month and local day are then parsed and saved to currentMonth and currentDay variables respectively.

A conditional check is then performed which checks if currentMonth is equal to ‘February’ and the currentDay is equal to 29. If both conditions are true, it prints Happy Leap Day! to the console. If currentMonth is not ‘February’ or currentDay is not 29, then it prints Today is not a leap day.

The repo can be found here leap-day.

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	fmt.Println(now.Format(time.UnixDate))

	currentMonth := now.Local().Month()
	currentDay := now.Local().Day()

	if currentMonth.String() == "February" && currentDay == 29 {
		fmt.Println("Happy Leap Day!")
	} else {
		fmt.Println("Today is not a leap day.")
	}

}

Output

Running the program prints the current time in UnixDate format Which looks like this “Mon Jan 2 15:04:05 MST 2006” and short message which confirms it is a leap day or not.

Thu Feb 29 09:00:00 CET 2024
Happy Leap Day!

On non-leap days, this message is printed instead.

Wed Feb 28 11:05:00 CET 2024
Today is not a leap day.

Main Takeaway

This nifty Go program will tell you if the current day is a leap day.

Leap Day is like the Olympics of Non-Government Recognized Holidays. It only comes around every four years so make it count. Happy Leap Day!