β

golang时间格式转换

birdcat's blog 183 阅读
  1. 将YYYYmmddHHDD类型的时间转化成unix时间戳

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        stringTime := "201601010000"
        tmpTime, _ := time.ParseInLocation("200601021504", stringTime, time.Local)
        unixTime := tmpTime.Unix()
        fmt.Printf("The Unix time of %v is %v\n", stringTime, unixTime)
    }
    
  2. 将unix时间戳转化成YYYYmmddHHDD格式的时间

    package main
    
    import (
        "fmt"
        "strconv"
        "time"
    )
    
    func main() {
        i, err := strconv.ParseInt("1405544146", 10, 64)
        if err != nil {
            panic(err)
        }
        tm := time.Unix(i, 0)
        fmt.Println(tm.Format("200601021504"))
    }
    
作者:birdcat's blog
birdcat's blog - The home of bird and cat.
原文地址:golang时间格式转换, 感谢原作者分享。