时间函数

字符串转时间戳

本地时间和0时区的差距

str := "2017-03-04 12:11:45"
res, err := time.Parse("2006-01-02 15:04:05", str)
if err == nil {
	fmt.Println(res.Unix())
}

本地时间

str := "2017-03-04 12:11:45"
res2, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
if err == nil {
	fmt.Println(res2.Unix())
}

字符串转时间戳另一种方法

the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local)
unix_time := the_time.Unix()
fmt.Println(unix_time)

获取时间戳

timestamp := time.Now().Unix()
fmt.Println(timestamp)

格式化为字符串,tm为Time类型

tm := time.Unix(timestamp, 0)
fmt.Println(tm.Format("2006-01-02 03:04:05 PM"))
fmt.Println(tm.Format("02/01/2006 15:04:05 PM"))

当前格式化时间

fmt.Println(time.Now().Format("2006-01-02 15:04:05"))

time.Time转成unix

time.Time.Unix()

unix转成time.Time

time.Unix(time.Time, 0) 
Last Updated:
Contributors: huangyanfu