Go 处理时间


package main

import (
    "fmt"
"strconv"
    "time"
)

// TimeUnix is time for unix
func TimeUnix() string {
naiveTime := time.Now().Unix()
naiveTimeString := strconv.FormatInt(naiveTime, 10)
return naiveTimeString
}

func main() {
    timeNow := time.Now()
    fmt.Println("tNow(time format): ", timeNow)
    
    // 时间转化为 string layout 必须为 "2006-01-02 15:04:05"
    timeNowStr := timeNow.Format("2006-01-02 15:04:05")
    fmt.Println("tNow(string format): ", timeNowStr)

    timeNowUnixInt := timeNow.Unix()
    fmt.Println(timeNowUnixInt)

    // string 转化为时间 layout 必须为 "2006-01-02 15:04:05"
    t1, _ := time.Parse("2006-01-02 15:04:05", "2014-06-15 08:37:18")
    fmt.Println("t(time format): ", t1)
    t2 := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")
    fmt.Println("t(time format): ", t2)
}
分享:

评论