1 Star 3 Fork 0

未名/gocalendar

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
datetime.go 868 Bytes
一键复制 编辑 原始数据 按行查看 历史
未名 提交于 4年前 . v1.1.0 初始
package gocalendar
import (
"time"
)
// GregorianMonthDays 公历某月的总天数
func GregorianMonthDays(year, month int) int {
md := [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if month == 2 && IsLeapYear(year) {
return 29
}
return md[month - 1]
}
// IsLeapYear 给定的公历年year是否是闰年
func IsLeapYear(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
// BeginningOfMonth 计算出t所在月份的首日
//
// 时hour,分minute,秒second与t对应的值一样
func BeginningOfMonth(t time.Time) time.Time {
day := t.Day()
day--
return t.AddDate(0,0,-day)
}
// EndOfMonth 计算出t所在月份的最后一日
//
// 时hour,分minute,秒second与t对应的值一样
func EndOfMonth(t time.Time) time.Time {
nextMonthTime := t.AddDate(0,1,0)
return BeginningOfMonth(nextMonthTime).AddDate(0,0,-1)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/phpu/gocalendar.git
git@gitee.com:phpu/gocalendar.git
phpu
gocalendar
gocalendar
master

搜索帮助