2 回答

TA貢獻1776條經驗 獲得超12個贊
請閱讀time.Parse的文檔:
布局通過顯示參考時間如何定義格式,定義為
2006 年 1 月 2 日星期一 15:04:05 -0700 MST
如果它是值,將被解釋;它用作輸入格式的示例。然后將對輸入字符串進行相同的解釋。
所以正確的格式是
t, err := time.Parse("2006-01-02", "2011-01-19")

TA貢獻1834條經驗 獲得超8個贊
除了使用文字2006-01-02時間格式之外,您還可以通過創建一個類似于 Go 在time包中的做法的常量來減少錯誤。
YYYY-MM-DD格式在 RFC-3339 中定義如下(full-date調整順序):
full-date = date-fullyear "-" date-month "-" date-mday
date-fullyear = 4DIGIT
date-month = 2DIGIT ; 01-12
date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on
; month/year
time.RFC3339因此,您可以創建一個如下所示的常量來與內置time.RFC3339Nano常量一起使用。
const RFC3339FullDate = "2006-01-02"
然后您可以編寫以下內容:
t, err := time.Parse(RFC3339FullDate, "2011-01-19")
這是在gotilla/time/timeutil包中,所以你可以寫:
t, err := time.Parse(timeutil.RFC3339FullDate, "2011-01-19")
作為參考,time/format.go包含以下常量:
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
- 2 回答
- 0 關注
- 150 瀏覽
添加回答
舉報