Go Time常用语法
生活随笔
收集整理的這篇文章主要介紹了
Go Time常用语法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Go Time常用語法
目錄
1. 獲取當(dāng)前時間戳和string類型的格式化時間
1. 獲取當(dāng)前時間戳
func GetCurTimestamp() int64 {return time.Now().Unix() }2. 獲取格式化時間
func GetCurTimeFormat() string {return time.Now().Format("2006-01-02 15:04:05") //格式時間不能改其他,會到時時間不準(zhǔn)確 }2. 構(gòu)造指定時間
//獲取指定時間 func GetAppointTime() string {now := time.Now()return time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.Local).Format("2006-01-02 15:04:05") //參數(shù)時間可修改 }3. 時間戳和格式化時間相互轉(zhuǎn)換
1. 時間戳轉(zhuǎn)格式化時間
//時間戳轉(zhuǎn)格式化時間 func GetUnix2Format(timestamp int64) string {return time.Unix(timestamp,0).Format("2006-01-02 15:04:05") }2. 格式化時間轉(zhuǎn)時間戳
//格式化時間轉(zhuǎn)時間戳 func GetFormat2Unix(formatTime string) int64 {unixTime, _ := time.ParseInLocation("2006-01-02 15:04:05", formatTime, time.Local) //使用parseInLocation將字符串格式化返回本地時區(qū)時間return unixTime.Unix() }4. 獲取當(dāng)前幾月,幾號,星期幾
//獲取幾月 func GetMonthNum() int {return int(time.Now().Month()) }//獲取幾號 func GetDayNum() int {return time.Now().Day() }//獲取星期幾 func GetWeekNum() int {return int(time.Now().Weekday()) }總結(jié)
以上是生活随笔為你收集整理的Go Time常用语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ElasticSearch搜索语法进阶学
- 下一篇: FLV格式分析