Golang时间错误:月份超出范围

时间:2016-09-20 08:49:16

标签: go

这是我的代码:

time.Parse(time.Now().String()[0:19],time.Now().String()[0:19])

错误:

parsing time "2016-09-20 16:50:08": month out of range

如何解析时间字符串?

2 个答案:

答案 0 :(得分:1)

第一个参数是布局,请参阅:

func Parse(layout, value string) (Time, error) {
    return parse(layout, value, UTC, Local)
}

文档:

// 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
// would be interpreted if it were the value; it serves as an example of
// the input format. The same interpretation will then be made to the
// input string.
//
// Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard
// and convenient representations of the reference time. For more information
// about the formats and the definition of the reference time, see the
// documentation for ANSIC and the other constants defined by this package.
// Also, the executable example for time.Format demonstrates the working
// of the layout string in detail and is a good reference.
//
// Elements omitted from the value are assumed to be zero or, when
// zero is impossible, one, so parsing "3:04pm" returns the time
// corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is
// 0, this time is before the zero Time).
// Years must be in the range 0000..9999. The day of the week is checked
// for syntax but it is otherwise ignored.
//
// In the absence of a time zone indicator, Parse returns a time in UTC.
//
// When parsing a time with a zone offset like -0700, if the offset corresponds
// to a time zone used by the current location (Local), then Parse uses that
// location and zone in the returned time. Otherwise it records the time as
// being in a fabricated location with time fixed at the given zone offset.
//
// No checking is done that the day of the month is within the month's
// valid dates; any one- or two-digit value is accepted. For example
// February 31 and even February 99 are valid dates, specifying dates
// in March and May. This behavior is consistent with time.Date.
//
// When parsing a time with a zone abbreviation like MST, if the zone abbreviation
// has a defined offset in the current location, then that offset is used.
// The zone abbreviation "UTC" is recognized as UTC regardless of location.
// If the zone abbreviation is unknown, Parse records the time as being
// in a fabricated location with the given zone abbreviation and a zero offset.
// This choice means that such a time can be parsed and reformatted with the
// same layout losslessly, but the exact instant used in the representation will
// differ by the actual zone offset. To avoid such problems, prefer time layouts
// that use a numeric zone offset, or use ParseInLocation.

您可以使用

t, err := time.Parse("2006-01-02 15:04:05", time.Now().String()[:19])

试试The Go Playground

package main

import (
    "fmt"
    "time"
)

func main() {
    t, err := time.Parse("2006-01-02 15:04:05", time.Now().String()[:19])
    if err != nil {
        panic(err)
    }
    fmt.Println(t)
}

输出:

2009-11-10 23:00:00 +0000 UTC

答案 1 :(得分:0)

我遇到了同样的问题,所以我来这里说golang有时会表示“月份”,而表示“月份的日期”,错误消息是错误的,下面是一个示例:

package main

import (
    "fmt"
    "time"
)

func main() {

dateAsString:= "31/Oct/2019"

layout := "01/Jan/2006" // BAD BAD BAD SHOULD BE 02 INSTEAD OF 01
fmt.Println("INPUT:" + dateAsString)

t, err := time.Parse(layout, dateAsString)
if err != nil {
    fmt.Println("DATE UNPARSEABLE:3", err)

}
fmt.Println(t)

}