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

来源:互联网 发布:java打印金字塔型数字 编辑:程序博客网 时间:2024/06/05 07:10
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)
}

}
原创粉丝点击