duanbing6955
2018-06-07 18:04
浏览 285
已采纳

解析UTC日期字符串并转换为其他格式

Go newcomer here.

I have a date string 2018-06-07T16:16:57Z and I want to convert it to something like this mm/dd/yyyy hh:mm.

This seems to be a frequently asked question, but I can't seem to find any previous questions that work for me.

I'm reading in a time field and trying to convert like this

time := row["Date & Time"]
fmt.Println(time)
t, _ := time.Parse("2006-01-02 15:04:05 -0700 UTC", time)
fmt.Println(t)

But I think the issue is that I don't have a correct format string. I've tried a few resources to no success.

When I print t as is, I get 0001-01-01 00:00:00 +0000 UTC as a result, which is obviously incorrect.

What I'd like to do is convert the time I'm reading in like this

newTime := currentDate.Format("01/02/2006 hh:mm")

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doumao1887 2018-06-07 18:19
    已采纳

    You have two issues.

    First, you should not name the variable time as that is the name of a built-in package. I suppose you knew that and this is just a copy paste error.

    Next, the string you pass to time.Parse() is a format string that should describe the format of the time string from your database. You already know what the format is: 2018-06-07T16:16:57Z, so just use that replacing the value with Go's reference time.

    Here is working variant:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        tm := "2018-06-07T16:16:57Z"
        fmt.Println(tm)
        t, err := time.Parse("2006-01-02T15:04:05Z", tm)
        if err != nil {
            panic(err)
        }
        fmt.Println(t)
    }
    

    Run in playground


    What's more the time format database uses is often described as RFC3339, which is also available as the time.RFC3339 constant in Go.

    So using that simplifies your code even further:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        tm := "2018-06-07T16:16:57Z"
        fmt.Println(tm)
        t, err := time.Parse(time.RFC3339, tm)
        if err != nil {
            panic(err)
        }
        fmt.Println(t)
    }
    

    Run in playground


    And if you prefer, you could also let the database driver convert the time for you by scanning it to a time.Time variable.

    For example:

    var tm time.Time
    if err = row.Scan(&tm); err != nil {
        panic(err)
    }
    fmt.Print(tm)
    
    点赞 打赏 评论
  • dongzen7263 2018-06-07 18:18

    As @mkopriva describes the layout should be matched. Understand the layout which is 2006-01-02T15:04:05Z, go through Golang spec for layout use to convert the string to date

    Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be

    Mon Jan 2 15:04:05 -0700 MST 2006
    

    and then use returned time value to Format the date according to your requirement.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        layout1 := "2006-01-02T15:04:05Z"
        t, err := time.Parse(layout1, "2018-06-07T16:16:57Z")
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(t.Format("01/02/2006 15:04"))
    }
    

    Check it on Go Playground

    点赞 打赏 评论

相关推荐 更多相似问题