2017-02-14 24 views
1

給定一個時間變量,我想打印年,月和日。 從文檔看來,似乎可以使用任何佈局。例如,我看不到佈局之間的區別2006-01-02,2006-10-10,1999-02-02。轉:time.Format:如何理解'2006-01-02'佈局的含義?

但是,只有佈局2006-01-02返回我所期望的。 我在哪裏可以找到關於佈局中'2006','01','02'含義的文檔?

我在這裏打有不同的佈局:go playground: testing layouts

+0

你問:「我在哪裏可以找到[p一個函數f中的函數f]佈局中的[...一些細節]的含義文檔?」答案總是相同的:在包p的文檔中(總是閱讀完整的包文檔!)和函數f的文檔(也可以讀兩遍)。文檔沒有短路:不要猜測,不要假設基於簽名,不要單獨在函數doc上進行實驗:先閱讀包文檔,然後閱讀函數doc。必要時重複。運行示例。 – Volker

回答

5

Mon Jan 2 15:04:05 -0700 MST 2006是參考時間,這意味着佈局需要使用確切的日期。有more information here,但基本上通過使用日期時間的每個部分的唯一值,它能夠知道每個部分(年,月等)實際上是自動的。

Corrected go playground

3

跟進傑克的信息,請參閱詳細examples

// The layout string used by the Parse function and Format method 
// shows by example how the reference time should be represented. 
// We stress that one must show how the reference time is formatted, 
// not a time of the user's choosing. Thus each layout string is a 
// representation of the time stamp, 
// Jan 2 15:04:05 2006 MST 
// An easy way to remember this value is that it holds, when presented 
// in this order, the values (lined up with the elements above): 
// 1 2 3 4 5 6 -7 

這個參考時間可以讓我們澄清是否應該去解析17年1月2日爲2017年1月2日或二月1

+0

謝謝,這解釋了一切:「我們強調必須顯示參考時間是如何格式化的,而不是用戶選擇的時間。」可悲的是,這樣的重要信息在這個例子中是隱藏的 – mkokho