go 语言中 日期转换 日期、时间戳、字符串 的转换(这个是最终答案)

xiaoxiao2021-02-28  16

import ( "fmt" "time" ) func main() { //返回现在时间 Time 时间类型 timeNow := time.Now() //2012-10-31 15:50:13.793654 +0000 UTC //Time 时间转化为string timeString := timeNow.Format("2006-01-02 15:04:05") //2015-06-15 08:52:32 //获取时间戳 timestamp := time.Now().Unix() //1504079553 //时间戳转Time 再转 string timeNow := time.Unix(timestamp, 0) //2017-08-30 16:19:19 +0800 CST timeString := timeNow.Format("2006-01-02 15:04:05") //2015-06-15 08:52:32 //string 转 时间戳 stringTime := "2017-08-30 16:40:41" loc, _ := time.LoadLocation("Local") the_time, err := time.ParseInLocation("2006-01-02 15:04:05", stringTime, loc) if err == nil { unix_time := the_time.Unix() //1504082441 fmt.Println(unix_time) } }
转载请注明原文地址: https://www.6miu.com/read-1250027.html

最新回复(0)