用途
获取指定年份/月份中的所有天数
package main
import (
"fmt"
"time"
)
func main() {
days := make([]string, 0)
year := time.Now().Year()
for month := 1; month <= 12; month++ {
for day := 1; day <= 31; day++ {
//如果是2月
if month == 2 {
if isLeapYear(year) && day == 30 { //闰年2月29天
break
} else if !isLeapYear(year) && day == 29 { //平年2月28天
break
} else {
days = append(days,fmt.Sprintf("%d-%02d-%02d",year,month,day))
}
} else if month == 4 || month == 6 || month == 9 || month == 11 { //小月踢出来
if day == 31 {
break
}
days = append(days,day))
} else {
days = append(days,day))
}
}
}
fmt.Println(days)
}
//判断是否为闰年
func isLeapYear(year int) bool { //y == 2000,2004
//判断是否为闰年
if year%4 == 0 && year%100 != 0 || year%400 == 0 {
return true
}
return false
}