现在有一些关键的价值观.Parse正在寻找.
通过更改:
test, err := time.Parse("10/15/1983", "10/15/1983")
至
test, err := time.Parse("01/02/2006", "10/15/1983")
解析器会识别它.
这是modified code on the playground美元.
package main
import "fmt"
import "time"
func main() {
test, err := time.Parse("01/02/2006", "10/15/1983")
if err != nil {
panic(err)
}
fmt.Println(test)
}
You can utilize the constants list in the src/pkg/time/format.go file 至 create your own parse formats.
const (
stdLongMonth = "January"
stdMonth = "Jan"
stdNumMonth = "1"
stdZeroMonth = "01"
stdLongWeekDay = "Monday"
stdWeekDay = "Mon"
stdDay = "2"
stdUnderDay = "_2"
stdZeroDay = "02"
stdHour = "15"
stdHour12 = "3"
stdZeroHour12 = "03"
stdMinute = "4"
stdZeroMinute = "04"
stdSecond = "5"
stdZeroSecond = "05"
stdLongYear = "2006"
stdYear = "06"
stdPM = "PM"
stdpm = "pm"
stdTZ = "MST"
stdISO8601TZ = "Z0700" // prints Z for UTC
stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
stdNumTZ = "-0700" // always numeric
stdNumShortTZ = "-07" // always numeric
stdNumColonTZ = "-07:00" // always numeric
)
So anytime your format specifies a year, it should be done with "06" or "2006", seconds are specified by "05" or "5" and time zones are specified at "MST", "Z0700", "Z07:00", "-0700", "-07" or "-07:00". If you reference the constants list you can likely put 至gether any standard format you'd need 至 parse.
For example, if you want 至 parse the date/time in the Common Log Format, the format Apache uses for its log files, you would do so by passing the following string 至 time.Parse()
as the layout argument.
"02/Jan/2006:15:04:05 -0700"
"02"表示月份字段,"Jan"表示月份名称字段,"2006"表示年份字段,"15"表示24小时格式的小时字段,"04"表示分钟字段,"05"表示秒字段,-0700"表示时区字段.
该格式将解析当前PST时间:31/Dec/2012:15:32:25 -0800
所以time.Parse()
呼叫应该是这样的:
test, err := time.Parse("02/Jan/2006:15:04:05 -0700", "31/Dec/2012:15:32:25 -0800")