dshmkgq558192365 2016-08-05 21:33
浏览 159
已采纳

将UTC字符串转换为时间对象

I have this datetime, or something that looks like it.

2014-11-17 23:02:03 +0000 UTC

I want to convert this to a time object and I've been unable to produce any output from time.Parse apart from:

0001-01-01 00:00:00 +0000 UTC

I've tried these layouts:

time.RFC3339
0001-01-01 00:00:00 0000 UTC
2016-10-10
time.UnixDate

And a few more - none have worked.

This is how I'm calling parse :

updatedAt, err := time.Parse(time.UnixDate, updatedAtVar)

How do I create a time object from a string?

  • 写回答

2条回答 默认 最新

  • donglaogu3788 2016-08-05 21:37
    关注

    Most likely you used a wrong layout, and you didn't check the returned error.

    The layout must be this date/time, in the format your input time is:

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

    See this working code:

    layout := "2006-01-02 15:04:05 -0700 MST"
    t, err := time.Parse(layout, "2014-11-17 23:02:03 +0000 UTC")
    fmt.Println(t, err)
    

    Output (try it on the Go Playground):

    2014-11-17 23:02:03 +0000 UTC <nil>
    

    EDIT:

    In your question you included a + sign in your input time (as part of the zone offset), but you have error with times of other formats.

    Time.String() uses the following format string:

    "2006-01-02 15:04:05.999999999 -0700 MST"
    

    So either use this to parse the times, or use Time.Format() to produce your string representations where you can specify the layout, so you can use the same layout to parse the time strings.

    2nd round:

    You're including your time strings in URLs. The + sign is a special character in URL encoding: it denotes the space. So the + gets converted to space (and so it vanishes from your time string). Use proper URL encoding! Check out the net/url package, and this example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douzhaolu4780 2016-08-05 21:44
    关注

    You are likely using the wrong layout. As explained in time.Parse, you need to specify a layout that helps Go to understand how the date passed as input is formatted.

    There are predefined layouts (like the ones you were using), but none matches your input. Hence you need to define a custom layout.

    A layout uses the following date as reference:

    Mon Jan 2 15:04:05 MST 2006
    

    The layout is nothing else that a representation of that date, that matches the representation of your input:

    t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", "2014-11-17 23:02:03 +0000 UTC")
    

    Also remember to check err for errors. It's likely your attempts returned an error, but you didn't check it.

    package main
    
    import (
        "fmt"
        "log"
        "time"
    )
    
    func main() {
        t, err := time.Parse("2006-01-02 15:04:05 -0700 UTC", "2014-11-17 23:02:03 +0000 UTC")
        if err != nil {
            log.Fatalln(err)
        }
        fmt.Println(t)
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电脑连不上无线网络如下诊断反馈应该如何操作
  • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
  • ¥15 在ubuntu中无法连接到远程服务器传输文件
  • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
  • ¥15 Oracle触发器字段变化时插入指定值
  • ¥15 docker无法进入容器内部
  • ¥15 qt https 依赖openssl 静态库
  • ¥15 python flask 报错
  • ¥15 改个密码引发的项目启动问题
  • ¥100 CentOS7单线多拨