Date parsing in Go

前端 未结 1 1354
北恋
北恋 2020-12-09 17:44

I\'m trying to parse a timestamp as produced by tar such as \'2011-01-19 22:15\' but can\'t work out the funky API of time.Parse.

The following produces \'parsing ti

相关标签:
1条回答
  • 2020-12-09 18:07

    Follow the instructions in the Go time package documentation.

    The standard time used in the layouts is:

    Mon Jan 2 15:04:05 MST 2006 (MST is GMT-0700)

    which is Unix time 1136243045. (Think of it as 01/02 03:04:05PM '06 -0700.) To define your own format, write down what the standard time would look like formatted your way.

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t, err := time.Parse("2006-01-02 15:04", "2011-01-19 22:15")
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(time.SecondsToUTC(t.Seconds()))
    }
    
    Output: Wed Jan 19 22:15:00 UTC 2011
    
    0 讨论(0)
提交回复
热议问题