您的位置:首页 > 编程语言 > Go语言

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

2017-08-30 16:49 567 查看
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)
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  go 日期 转换