Go语言标准库time包

golang标准库time

time包提供时间显示和时间间隔的相关功能。

基本使用

打印当前时间,基本使用实例:

func main() {
now := time.Now()
fmt.Printf("now=%v,now type=%T\n",now,now)
//now=2022-05-03 11:24:14.5336364 +0800 CST m=+0.003997601,now type=time.Time
//2.通过now可以获取年月日时分秒
fmt.Printf("年=%v\n",now.Year())   //年
fmt.Printf("月=%v\n",now.Month()) //月
fmt.Printf("月=%v\n",int(now.Month())) //月
fmt.Printf("日=%v\n",now.Day())   //日
fmt.Printf("时=%v\n",now.Hour()) //时
fmt.Printf("分=%v\n",now.Minute()) //分
fmt.Printf("秒=%v\n",now.Second()) //秒

//3.格式化日期时间
fmt.Printf("当前年月日:%d-%d-%d %d:%d:%d\n",now.Year(),
now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())

dateStr := fmt.Sprintf("当前年月日:%d-%d-%d %d:%d:%d",now.Year(),
now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())

fmt.Printf("dateStr=%v\n",dateStr)
}

时间戳

Unix Time是自1970年1月1日 00:00:00 UTC 至当前时间经过的总秒数。下面的代码片段演示了如何基于时间对象获取到Unix 时间。

// timestampDemo 时间戳
func timestampDemo() {
now := time.Now()       // 获取当前时间
timestamp := now.Unix() // 秒级时间戳
nano := now.UnixNano() // 纳秒时间戳
fmt.Printf("timestamp: %v\n", timestamp)   //timestamp: 1651549033
fmt.Printf("nano: %v\n", nano) //nano: 1651549033215561300
}

时间戳转化为普通的时间格式

在go中time.Unix来直接将时间戳转化为当前时间格式,实现转换。

func timeStampToTime() {
timestamp := time.Now().Unix()
timeObj := time.Unix(timestamp, 0) //timeObj: 2022-05-03 11:44:57 +0800 CST
fmt.Printf("timeObj: %v\n", timeObj)
year := timeObj.Year()
month := timeObj.Month()
day := timeObj.Day()
hour := timeObj.Hour()
minute := timeObj.Minute()
second := timeObj.Second()
fmt.Printf("year: %v\n", year) //year:2022
fmt.Printf("month: %v\n", month)
fmt.Printf("day: %v\n", day)
fmt.Printf("hour: %v\n", hour)
fmt.Printf("minute: %v\n", minute)
fmt.Printf("second: %v\n", second)
}

以上代码执行,结果如下:

timestamp: 1651549667
nano: 1651549667256716800
timeObj: 2022-05-03 11:47:47 +0800 CST
year: 2022
month: May
day: 3
hour: 11
minute: 47
second: 47

时间操作

此处不能增加年、月、日,只能增加时分秒,以下常量才会被允许。

const (
  Nanosecond Duration = 1
  Microsecond         = 1000 * Nanosecond
  Millisecond         = 1000 * Microsecond
  Second               = 1000 * Millisecond
  Minute               = 60 * Second
  Hour                 = 60 * Minute
)
Add
func add() {
now := time.Now()
hour := time.Hour //hour= 1h0m0s
later := now.Add(time.Hour)   //当前时间加1小时后的时间
fmt.Printf("later: %v\n", later)
fmt.Println("hour=", hour)
}

Sub

可用于计算时间间隔。

func Sub() {
startTime := time.Now()
time.Sleep(time.Millisecond * 5)
fmt.Println("时间间隔为:", time.Now().Sub(startTime)) //时间间隔为: 5.966ms
}

时间格式化

time.Format函数能够将一个时间对象格式化输出为指定布局的文本表示形式,需要注意的是 Go 语言中时间格式化的布局不是常见的Y-m-d H:M:S,而是使用 2006-01-02 15:04:05.000

其中:

  • 2006:年(Y)

  • 01:月(m)

  • 02:日(d)

  • 15:时(H)

  • 04:分(M)

  • 05:秒(S)

func timeFormat() {
now := time.Now()
// format方法
fmt.Printf(now.Format("2006/01/02 15:04:05")) //时间固定不能改,但是格式可以改
fmt.Println()
fmt.Printf(now.Format("2006-01-02"))
fmt.Println()
fmt.Printf(now.Format("15:04:05"))
}

结果:
2022/05/03 12:14:37
2022-05-03
12:14:37

补充:如果想格式化为12小时制,需指定PM

解析字符串格式的时间

now := time.Now()
fmt.Println("now=", now)
//加载时区
l, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println("err=", err)
return
}

//按照指定失去和指定时间格式解析字符串时间
t, err2 := time.ParseInLocation("2006/01/02 15:04:05", "2019/08/01 14:15:20", l)
if err2 != nil {
fmt.Println("err2=", err2)
return
}

fmt.Println("t=", t) //t= 2019-08-01 14:15:20 +0800 CST
fmt.Println(now.Sub(t)) //24142h9m30.9108286s
 
posted @ 2022-05-03 12:28  wushaoyu  阅读(127)  评论(0编辑  收藏  举报