dongxian1699
dongxian1699
2016-11-28 08:53
浏览 52
已采纳

在Golang中解析时间

I'm trying to create a Time struct based on some input called dateAdded. My code is like this:

dateAdded := "November 25, 2016"
layout := "September 9, 2016"
t, err := time.Parse(layout, dateAdded)
if err != nil {
    fmt.Println(err)
} else {
    fmt.Println(t)
}

And I get the following error: parsing time "November 25, 2016" as "September 9, 2016": cannot parse "November 25, 2016" as "September 9, "

I assume the Parse function cannot parse every layout, but I'm curios what's the usual way of reading dates and parse them into time objects.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试基于一些名为dateAdded的输入来创建Time结构。 我的代码是这样的:

  dateAdded:=“ 2016年11月25日” 
layout:=“ 2016年9月9日” 
t,err:= time.Parse(layout  ,dateAdded)
if err!= nil {
 fmt.Println(err)
}其他{
 fmt.Println(t)
} 
   
 
 

并且出现以下错误:将时间“ 2016年11月25日”解析为“ 2016年9月9日”:无法将“ 2016年11月25日”解析为“ 9月9日”, \ n

我认为Parse函数无法解析所有布局,但是我想知道什么是读取日期并将其解析为时间对象的常用方法。

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

3条回答 默认 最新

  • doubushi0031
    doubushi0031 2016-11-28 09:06
    已采纳

    Your layout date is wrong. Should be "January 2, 2006". As the specs say:

    The layout defines the format by showing how the reference time, defined to be Mon Jan 2 15:04:05 -0700 MST 2006 would be interpreted if it were the value

    点赞 评论
  • dongqie4402
    dongqie4402 2016-11-28 09:00

    The layout, if you're not using one of the pre-included constant layout that comes with the time module, must be formed from the exact timestamp Mon Jan 2 15:04:05 -0700 MST 2006. Notice that each element of it is unique, so each numeric identifier can be automatically parsed. it's basically 1 (month), 2 (day), 3 (hour), 4 (minute), 5 (second), 6 (year), 7 (time zone) etc.

    It's better to use one of the pre-defined standard layouts that are included with the library:

    const (
            ANSIC       = "Mon Jan _2 15:04:05 2006"
            UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
            RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
            RFC822      = "02 Jan 06 15:04 MST"
            RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
            RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
            RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
            RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
            RFC3339     = "2006-01-02T15:04:05Z07:00"
            RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
            Kitchen     = "3:04PM"
            // Handy time stamps.
            Stamp      = "Jan _2 15:04:05"
            StampMilli = "Jan _2 15:04:05.000"
            StampMicro = "Jan _2 15:04:05.000000"
            StampNano  = "Jan _2 15:04:05.000000000"
    )
    
    点赞 评论
  • dongsu0308
    dongsu0308 2016-11-28 09:32

    You should treat it as an example which you provide to time.Provide. And it should be of concrete value described in the documentation.

    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

    A playground with correct variant.

    点赞 评论

相关推荐