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