gpt4 book ai didi

Golang时间错误: month out of range

转载 作者:数据小太阳 更新时间:2023-10-29 03:14:47 37 4
gpt4 key购买 nike

这是我的代码:

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

错误:

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

如何解析时间字符串?

最佳答案

第一个参数是布局,见:

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

关于Golang时间错误: month out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39589594/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com