Golang获取时间戳和时间操作

时间:2025-03-02 07:57:51

时间戳   

().Unix()				//时间戳(秒) 
().UnixNano()			//时间戳(纳秒)
().UnixNano() / 1e6 	//时间戳(毫秒)
().UnixNano() / 1e9 	//时间戳(纳秒转换为秒)

注:1秒 = 1000毫秒 1毫秒 = 1000微秒 1微秒 = 1000纳秒  
1e6是指数表达形式:  1* 10的6次方

时间格式化

nowtime:=().Format("2006-01-02 15:04:05")  //要显示的时间格式,2006-01-02 15:04:05,这几个值是固定写法,golang的诞生时间.

(nowtime)    //打印结果:2021-02-02 13:22:04

时间戳与字符串相互转化

时间戳转时间字符串      

timeUnix:=().Unix() //时间戳

formatTimeStr:=(timeUnix,0).Format("2006-01-02 15:04:05")

(formatTimeStr) //打印结果:2021-02-02 13:22:04

 时间字符串转时间  

formatTimeStr := "2021-02-02 13:22:04" 

formatTime, err := ("2006-01-02 15:04:05", formatTimeStr)

(formatTime) //打印结果:2021-02-02 13:22:04 +0000 UTC