golang的time包
官方API
:
func Parse(layout, value string) (Time, error)
Code:
// See the example for time.Format for a thorough description of how
// to define the layout string to parse a time.Time value; Parse and
// Format use the same model to describe their input and output.
// longForm shows by example how the reference time would be represented in
// the desired layout.
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
fmt.Println(t)
// shortForm is another way the reference time would be represented
// in the desired layout; it has no time zone present.
// Note: without explicit zone, returns time in UTC.
const shortForm = "2006-Jan-02"
t, _ = time.Parse(shortForm, "2013-Feb-03")
fmt.Println(t)
Output:
2013-02-03 19:54:00 -0800 PST
2013-02-03 00:00:00 +0000 UTC
其中layout
的时间必须是"2006-01-02 15:04:05"
这个时间,不管格式如何,时间点一定得是这个,如:"Jan 2, 2006 at 3:04pm (MST)"
,"2006-Jan-02"
等。如换一个时间解析出来的时间就不对了,要特别注意这一点。