# Golang : How to determine if a year is leap year?

Tags : golang date leap-year

A leap year is a year with 366 days instead of 365 days. The algorithm to calculate if a year is a leap can be summarized like :

```
if year is divisible by 400 then
is_leap_year
else if year is divisible by 100 then
not_leap_year
else if year is divisible by 4 then
is_leap_year
else
not_leap_year
```

However, we are not going to implement this algorithm, but rather get the number of days in a given year and determine if that particular year is a leap year or not.

Here you go!

```
package main
import (
"fmt"
"time"
)
func IsLeapYear(y int) bool {
//Thirty days hath September,
//April, June and November;
//February has twenty eight alone
//All the rest have thirty-one
//Except in Leap Year, that's the time
//When February's Days are twenty-nine
// convert int to Time - use the last day of the year, which is 31st December
year := time.Date(y, time.December, 31, 0, 0, 0, 0, time.Local)
days := year.YearDay()
if days > 365 {
return true
} else {
return false
}
}
func main() {
fmt.Println("1998 is leap year? ", IsLeapYear(1998))
fmt.Println("1997 is leap year? ", IsLeapYear(1997))
fmt.Println("1996 is leap year? ", IsLeapYear(1996))
fmt.Println("1995 is leap year? ", IsLeapYear(1995))
}
```

Output :

1998 is leap year? false

1997 is leap year? false

1996 is leap year? true

1995 is leap year? false

References :

Tags : golang date leap-year

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+4.8k Golang : Bcrypting password

+1.7k Golang : Getting Echo framework StartAutoTLS to work

+1k Golang : How to calculate the distance between two coordinates using Haversine formula

+3.5k Golang : Set image canvas or background to transparent

+1.9k Golang : Find change in a combination of coins example

+21.5k Golang : Marshal and unmarshal json.RawMessage struct example

+4.4k Golang : Convert(cast) uintptr to string example

+6.6k Golang : How to check for empty array string or string?

+1.5k Mac OSX : Get disk partitions' size, type and name

+15.5k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions

+1.7k Golang : Number guessing game with user input verification example

+4.2k Golang : Force download file example