2016-09-20 7 views
0

のうちの月は私のコードです:Golang時エラーに:ここでは範囲

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

はエラー:

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

時刻文字列を解析する方法は?

答えて

1

まずparamがレイアウトされ、以下を参照してください。

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. 

あなたはThe Go Playground

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

てみ使用できます

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 
関連する問題