设置时区
var cstZone = time.FixedZone("CST", 8*3600) // 东八
time.Local = cstZone
// https://www.cnblogs.com/foxhappy/p/14577735.html
获取操作
当前时间
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
)
func main() {
fmt.Println(time.Now().In(cstZone).Format("2006-01-02 15:04:05"))
fmt.Println(time.Now().In(cstZone).Unix()) // 当前时间戳 1648108391
fmt.Println(time.Now().In(cstZone).UnixNano()) // 当前纳秒时间戳 1648108391123158200
fmt.Println(time.Now().In(cstZone).Nanosecond()) // 获取时间戳的纳秒部分 123678500
}
获取当前年月日时分秒、星期几、一年中的第几天等操作
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
format = "2006-01-02 15:04:05"
)
func main() {
now, _ := time.ParseInLocation(format, "2022-01-01 00:00:00", cstZone)
fmt.Printf("%d %d %d\n", now.Year(), now.Month(), now.Day()) // 获取年月日
fmt.Printf("%d %d %d\n", now.Hour(), now.Minute(), now.Second()) // 获取时分秒
fmt.Printf("%d \n", now.YearDay()) //获取一年中的第几天 1开始
now = now.AddDate(0, 0, 1)
fmt.Printf("%d \n", now.Weekday()) // 获取一周中的第几天, 周日=0
/*
2022 1 1
0 0 0
1
0
*/
}
转换操作
字符串转时间戳
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
)
func main() {
date, _ := time.ParseInLocation("2006-01-02 15:04:05",
time.Now().In(cstZone).Format("2006-01-02 15:04:05"),
cstZone)
fmt.Println(date.Unix())
}
时间戳转字符串
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
)
func main() {
date, _ := time.ParseInLocation("2006-01-02 15:04:05",
time.Now().In(cstZone).Format("2006-01-02 15:04:05"),
cstZone)
fmt.Println(time.Unix(date.Unix(), 0).Format("2006-01-02 15:04:05"))
}
日期和时间操作
获取过去的日期
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
timeFormat = "2006-01-02 15:04:05"
)
func main() {
fmt.Println(time.Now().In(cstZone).AddDate(0, 0, -1).Format(timeFormat)) // 获取昨天的日期
}
获取未来的日期
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
timeFormat = "2006-01-02 15:04:05"
)
func main() {
fmt.Println(time.Now().In(cstZone).AddDate(0, 0, 1).Format(timeFormat)) // 获取明天的日期
}
获取过去和未来的时间
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
format = "2006-01-02 15:04:05"
)
func main() {
now := time.Now().In(cstZone)
// before, _ := time.ParseDuration("-10s") // 获取10s之前的时间
// before, _ := time.ParseDuration("-10m") // 获取10m之前的时间
// before, _ := time.ParseDuration("-10h") // 获取10h之前的时间
before, _ := time.ParseDuration("10h10m10s") // 获取10h10m10s之后的时间
now = now.Add(before)
fmt.Println(before)
fmt.Println(now.Format(format))
}
时间比较操作
过去获未来
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
)
func main() {
// 如果 t 代表的时间点在 u 之前,返回真;否则返回假。
// func (t Time) Before(u Time) bool
// 如果 t 代表的时间点在 u 之后,返回真;否则返回假。
// func (t Time) After(u Time) bool
// 比较时间是否相等,相等返回真;否则返回假。
// func (t Time) Equal(u Time) bool
now := time.Now().In(cstZone)
yesterday := time.Now().In(cstZone).AddDate(0, 0, -1)
fmt.Println(now.After(yesterday))
fmt.Println(yesterday.Before(now))
fmt.Println(now.Equal(yesterday))
}
时间相差
package main
import (
"fmt"
"time"
)
var (
cstZone = time.FixedZone("CST", 8*3600) // 东八区
)
func main() {
// 返回当前时间与 t 的时间差,返回值是 Duration
// time.Since(t Time) Duration
// 返回 t 与当前时间的时间差,返回值是 Duration
// time.Until(t Time) Duration
now := time.Now().In(cstZone)
before, _ := time.ParseDuration("-1h")
now = now.Add(before)
fmt.Println(time.Since(now))
fmt.Println(time.Until(now))
}