Golang中時間格式化的實現(xiàn)詳解
Golang時間類型通過自帶的 Format 方法進行格式化。
需要注意的是Go語言中格式化時間模板不是常見的Y-m-d H:M:S
而是使用Go語言的誕生時間 2006-01-02 15:04:05 -0700 MST。
為了記憶方便,按照美式時間格式 月日時分秒年 外加時區(qū) 排列起來依次是 01/02 03:04:05PM ‘06 -0700,剛開始使用時需要注意。
實際項目中,F(xiàn)ormat 函數(shù)中可以自定義時間格式,也可以使用time包中的預定義格式:
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" )
time包中,定義了年、月、日、時、分、秒、周、時區(qū)的多種表現(xiàn)形式:
- 年: 06,2006
- 月份: 1,01,Jan,January
- 日: 2,02,_2
- 時: 3,03,15,PM,pm,AM,am
- 分: 4,04
- 秒: 5,05
- 周幾: Mon,Monday
- 時區(qū): -07,-0700,Z0700,Z07:00,-07:00,MST
根據(jù)以上提供的數(shù)據(jù),我們可以組合成多種格式化模板,示例代碼如下:
func main() { currentTime := time.Now() fmt.Println("當前時間 : ", currentTime) fmt.Println("當前時間字符串: ", currentTime.String()) fmt.Println("MM-DD-YYYY : ", currentTime.Format("01-02-2006")) fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02")) fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05")) fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2006#01#02")) fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05")) fmt.Println("Time with MicroSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000")) fmt.Println("Time with NanoSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000000")) fmt.Println("ShortNum Month : ", currentTime.Format("2006-1-02")) fmt.Println("LongMonth : ", currentTime.Format("2006-January-02")) fmt.Println("ShortMonth : ", currentTime.Format("2006-Jan-02")) fmt.Println("ShortYear : ", currentTime.Format("06-Jan-02")) fmt.Println("LongWeekDay : ", currentTime.Format("2006-01-02 15:04:05 Monday")) fmt.Println("ShortWeek Day : ", currentTime.Format("2006-01-02 Mon")) fmt.Println("ShortDay : ", currentTime.Format("Mon 2006-01-2")) fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5")) fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 PM")) fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 pm")) }
輸出結(jié)果:
當前時間 : 2020-06-01 10:10:46.1551731 +0800 CST m=+0.002992001
當前時間字符串: 2020-06-01 10:10:46.1551731 +0800 CST m=+0.002992001
MM-DD-YYYY : 06-01-2020
YYYY-MM-DD : 2020-06-01
YYYY.MM.DD : 2020.06.01 10:10:46
YYYY#MM#DD {Special Character} : 2020#06#01
YYYY-MM-DD hh:mm:ss : 2020-06-01 10:10:46
Time with MicroSeconds: 2020-06-01 10:10:46.155173
Time with NanoSeconds: 2020-06-01 10:10:46.155173100
ShortNum Month : 2020-6-01
LongMonth : 2020-June-01
ShortMonth : 2020-Jun-01
ShortYear : 20-Jun-01
LongWeekDay : 2020-06-01 10:10:46 Monday
ShortWeek Day : 2020-06-01 Mon
ShortDay : Mon 2020-06-1
Short Hour Minute Second: 2020-06-01 10:10:46
Short Hour Minute Second: 2020-06-01 10:10:46 AM
Short Hour Minute Second: 2020-06-01 10:10:46 am
到此這篇關于Golang中時間格式化的實現(xiàn)詳解的文章就介紹到這了,更多相關Golang時間格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
簡單了解Go語言中函數(shù)作為值以及函數(shù)閉包的使用
這篇文章主要介紹了簡單了解Go語言中函數(shù)作為值以及函數(shù)閉包的使用,是golang入門學習中的基礎知識,需要的朋友可以參考下2015-10-10golang原生http包實現(xiàn)各種情況的get請求方式
這篇文章主要介紹了golang原生http包實現(xiàn)各種情況的get請求方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Go 庫bytes.Buffer和strings.Builder使用及性能對比
這篇文章主要為大家介紹了Go 庫bytes.Buffer和strings.Builder使用及性能對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12GoLang并發(fā)機制探究goroutine原理詳細講解
goroutine是Go語言提供的語言級別的輕量級線程,在我們需要使用并發(fā)時,我們只需要通過 go 關鍵字來開啟 goroutine 即可。這篇文章主要介紹了GoLang并發(fā)機制goroutine原理,感興趣的可以了解一下2022-12-12