parse date in golang

后端 未结 1 1134
抹茶落季
抹茶落季 2021-01-15 04:38

Please help me to parse date like 2018-12-17 12:55:50 +0300 in golang

I\'m trying with layout

layout := \"2006-01-02 15:04:05 +0000\"
         


        
相关标签:
1条回答
  • 2021-01-15 05:06

    Package time

    import "time"

    the [layout] reference time can be thought of as

    01/02 03:04:05PM '06 -0700
    

    Use -0700, not +0000.

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        layout := "2006-01-02 15:04:05 -0700"
        dateString := "2018-12-17 12:55:50 +0300"
        t, err := time.Parse(layout, dateString)
        if err != nil {
            fmt.Println("Error while parsing date :", err)
        }
        fmt.Println(t.Format("2006-01-02 15:04:05"))
    }
    

    Playground: https://play.golang.org/p/Xr8zaTjODgC

    Output:

    2018-12-17 12:55:50
    
    0 讨论(0)
提交回复
热议问题