timepkg

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

time

时间格式化、解析、时间范围和常用时间计算。

基础用法

today := timepkg.Today()
end := timepkg.EndOfDay(time.Now())
text := timepkg.FormatRFC3339(time.Now())

注意事项

跨时区业务要明确 location;不要隐式依赖本机时区。

验证

go test ./time

Documentation

Index

Constants

View Source
const (
	RFC3339           = time.RFC3339               // 例子:2022-08-03T16:40:58+08:00
	YmdHmsTZ          = "2006-01-02T15:04:05-0700" // 例子:2022-08-03T16:40:58+0800
	YmdHms            = "2006-01-02 15:04:05"
	YmdHm             = "2006-01-02 15:04"
	YmdH              = "2006-01-02 15"
	Ymd               = "2006-01-02"
	Ym                = "2006-01"
	Y                 = "2006"
	YSecond           = "20060102150405"
	YMinute           = "200601021504"
	YHour             = "2006010215"
	YDay              = "20060102"
	YMonth            = "200601"
	MDay              = "0102"
	YmdHmsMillisecond = "2006-01-02 15:04:05.999"
	YmdHmsMicrosecond = "2006-01-02 15:04:05.999999"
	YmdHmsNanosecond  = "2006-01-02 15:04:05.999999999"
	YMillisecond      = "20060102150405.999"
	YMicrosecond      = "20060102150405.999999"
	YNanosecond       = "20060102150405.999999999"
	YmdHmsMLogger     = "2006-01-02T15:04:05.999"

	YmdHmsChinese = "2006年01月02日 15:04:05"
	YmdChinese    = "2006年01月02日"
	HmsChinese    = "15时04分05秒"
)

Date Format

Variables

This section is empty.

Functions

func AddDays

func AddDays(t time.Time, days int) time.Time

AddDays 增加天数(支持负数)

func AddMonths

func AddMonths(t time.Time, months int) time.Time

AddMonths 增加月数(支持负数)

func DateToTime

func DateToTime(format, date string) (time.Time, error)

DateToTime date to time

func DaysBetween

func DaysBetween(a, b time.Time) int

DaysBetween 计算两个时间之间的天数差(绝对值) 使用 UTC 日期差计算,避免夏令时导致的小时偏差

func DaysInMonth

func DaysInMonth(year int, month time.Month) int

DaysInMonth 获取指定年月的天数

func EndOfDay

func EndOfDay(t time.Time) time.Time

EndOfDay 获取指定时间当天的 23:59:59

func FormatRFC3339

func FormatRFC3339(t time.Time) string

FormatRFC3339 to RFC3339

func FriendlyDuration

func FriendlyDuration(d time.Duration) string

FriendlyDuration 将 Duration 转为友好的中文描述 例如:1h30m → "1小时30分钟",72h → "3天"

func IsAfter

func IsAfter(a, b time.Time) bool

IsAfter 判断 a 是否在 b 之后

func IsBefore

func IsBefore(a, b time.Time) bool

IsBefore 判断 a 是否在 b 之前

func IsBetween

func IsBetween(t, start, end time.Time) bool

IsBetween 判断 t 是否在 start 和 end 之间(包含边界)

func IsExpired

func IsExpired(t time.Time) bool

IsExpired 判断指定时间是否已过期(在当前时间之前)

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear 判断是否为闰年

func IsThisMonth

func IsThisMonth(t time.Time) bool

IsThisMonth 判断是否在本月内

func IsThisWeek

func IsThisWeek(t time.Time) bool

IsThisWeek 判断是否在本周内(周一为一周起始)

func IsThisYear

func IsThisYear(t time.Time) bool

IsThisYear 判断是否在本年内

func IsToday

func IsToday(t time.Time) bool

IsToday 判断是否是今天

func IsWeekend

func IsWeekend(t time.Time) bool

IsWeekend 判断是否为周末(周六或周日)

func IsWorkday

func IsWorkday(t time.Time) bool

IsWorkday 判断是否为工作日(周一至周五)

func IsYesterday

func IsYesterday(t time.Time) bool

IsYesterday 判断是否是昨天

func IsZero

func IsZero(t time.Time) bool

IsZero 判断时间是否为零值

func MillisToTime

func MillisToTime(ms int64) time.Time

MillisToTime 毫秒时间戳转 time.Time

func MonthEnd

func MonthEnd(t time.Time) time.Time

MonthEnd 获取指定时间所在月的最后一天 23:59:59

func StartOfDay

func StartOfDay(t time.Time) time.Time

StartOfDay 获取指定时间当天的 00:00:00(等价于 ToDay)

func ThisMonth

func ThisMonth(t time.Time) time.Time

ThisMonth 2019-08-21 22:07:07 -> 2019-08-01 00:00:00

func ThisYear

func ThisYear(t time.Time) time.Time

ThisYear 2019-08-21 22:07:07 -> 2019-01-01 00:00:00

func Time9999

func Time9999() time.Time

func TimeAgo

func TimeAgo(t time.Time) string

TimeAgo 将时间转为"多久前"的友好描述 例如:刚刚、5分钟前、3小时前、昨天、3天前、2个月前、1年前

func TimestampMicros

func TimestampMicros() int64

TimestampMicros 获取微秒级时间戳

func TimestampMillis

func TimestampMillis() int64

TimestampMillis 获取毫秒级时间戳

func TimestampToDate

func TimestampToDate(u int64, format string) string

TimestampToDate timestamp to date

func TimestampToTime

func TimestampToTime(u int64) time.Time

TimestampToTime timestamp to Time

func ToDay

func ToDay(t time.Time) time.Time

ToDay 2019-08-21 22:07:07 -> 2019-08-21 00:00:00

func ToHour

func ToHour(t time.Time) time.Time

ToHour 2019-08-21 22:07:07 -> 2019-08-21 22:00:00

func ToMinute

func ToMinute(t time.Time) time.Time

ToMinute 2019-08-21 22:07:07 -> 2019-08-21 22:07:00

func Today

func Today() time.Time

Today 今天0时

func WeekEnd

func WeekEnd(t time.Time) time.Time

WeekEnd 获取指定时间所在周的周日 23:59:59

func WeekStart

func WeekStart(t time.Time) time.Time

WeekStart 获取指定时间所在周的周一 00:00:00

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL