import "time"
类型
type Time struct {
wall uint64
ext int64
loc *Location
}
type Month int
type Weekday int
type Duration int64
常用函数和方法
函数:
Now() Time 当前Time
Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time //返回一个设置的time类型
Since(t Time) Duration //time.Now().Sub(t)
Unix(sec int64, nsec int64) Time // 时间戳转时间 1sec = 1nsec * 1e6 , sec 10位时间戳
方法:
(t Time) Add(d Duration) Time // returns the time t+d.
(t Time) AddDate(years int, months int, days int) Time
(t Time) Sub(u Time) Duration //计算时间差
(t Time) Unix() int64 10位时间戳
(t Time) UnixNano() int64 16位时间戳
(t Time) Equal(u Time) bool // 比较两个time相等
(t Time) After(u Time) bool // reports whether the time instant t is after u.
(t Time) Before(u Time) bool // reports whether the time instant t is before u.
(t Time) IsZero() bool // reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
(t Time) UTC() Time // returns t with the location set to UTC.
(t Time) Local() Time // returns t with the location set to local time.
(t Time) In(loc *Location) Time //设置为指定location
(t Time) Location() *Location // returns the time zone information associated with t.
(t Time) Zone() (name string, offset int) // name of the zone (such as "CET") and its offset in seconds east of UTC.
time和string转换
time -> string
(t Time) Format(layout string) string // layout :("2006-01-02 15:04:05")
string -> time
Parse(layout, value string) (Time, error) // layout
获取本周周一零点时间戳(以周一为起始周)
timeNow := time.Now()
day := timeNow.Format("2006-01-02 15:04:05")
offset := int(timeNow.Weekday() - time.Weekday(1))
if offset == -1 {
offset = 6
}
timeNowZeroStr := timeNow.Format("2006-01-02")
t, _ := time.Parse("2006-01-02", timeNowZeroStr)
thisMonday := t.AddDate(0, 0, -offset).Unix()
有疑问加站长微信联系(非本文作者)