在我們編程過程中,經(jīng)常會用到與時(shí)間相關(guān)的各種務(wù)需求,下面來介紹 golang 中有關(guān)時(shí)間的一些基本用法。
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)高郵,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
golang可以使用日期時(shí)間包time設(shè)置時(shí)間,golang的日期時(shí)間包time的使用方式:
1、time package 包含了 time.Time 時(shí)間對象 及 構(gòu)建此時(shí)間對象的一些方法(time.Unix(), time.Parse())
2、golang 可精確到 nanosecond,故相應(yīng)的函數(shù)返回值或參數(shù)都已納秒為單位,我們可以借助time.ParseDuration(durationString string)友好的生成納秒度量的時(shí)間跨度值
3、golang 的時(shí)間格式串Layout固定為 2006-01-02 15:04:05
4、golang 默認(rèn)使用 Local 即本地時(shí)區(qū),可以通過 time.LoadLocation(zoneName string) (*Location, error)來設(shè)定時(shí)區(qū)
時(shí)區(qū)構(gòu)建/格式化模式串
time.LoadLocation 可以加載并返回一個(gè)時(shí)區(qū)變量
golang 使用2006-01-02 15:04:05做格式串,有人說是golang誕生時(shí)間,也有的說按月/日/時(shí)/分/秒/年的話是01/02/03(15-12)/04/05/06(2006),大家自行理解吧
// 構(gòu)建時(shí)區(qū) var timeLocation *time.Location timeLocation, _ = time.LoadLocation("") //UTC timeLocation, _ = time.LoadLocation("UTC") //UTC timeLocation, _ = time.LoadLocation("Local") //Local timeLocation, _ = time.LoadLocation("Asia/Shanghai") //使用時(shí)區(qū)碼 //golang的時(shí)間格式化pattern var timeLayout = "2006-01-02 15:04:05"
當(dāng)前時(shí)間對象
使用 time.Now() 方法可以獲取當(dāng)前時(shí)間對象,時(shí)間對象通過 time.Now().In()方法設(shè)定時(shí)區(qū),也可通過timer.Local()/timer.UTC()生成一個(gè)相應(yīng)時(shí)區(qū)的新的時(shí)間對象。
// 獲取當(dāng)前時(shí)間對象 var timer time.Time timer = time.Now() // 為時(shí)間設(shè)定時(shí)區(qū) 可以通過 timer.Local()/timer.UTC() 快速設(shè)定時(shí)區(qū) timer.In(timeLocation)
獲取秒級時(shí)間戳/納秒級時(shí)間戳
// 獲取當(dāng)前秒級時(shí)間戳 var curTimestamp int64 curTimestamp = timer.Unix() println("current timestamp:" + strconv.FormatInt(curTimestamp, 10)) // 獲取當(dāng)前納秒及時(shí)間戳 1秒=1000毫秒=1000,000微妙=1000,000,000納秒 var curNanoTimestamp int64 curNanoTimestamp = timer.UnixNano() println("current nano timestamp:" + strconv.FormatInt(curNanoTimestamp, 10))
獲取本地時(shí)間的時(shí)區(qū)/CST標(biāo)準(zhǔn)時(shí)間/自定義格式
// 獲取本地時(shí)間的時(shí)區(qū)/快速獲取時(shí)區(qū)時(shí)間/自定義格式 timeZone, _ := timer.Zone() fmt.Printf("time zone: %s\n", timeZone) fmt.Printf("time location: %s\n", timer.Location()) fmt.Printf("time in local zone: %s\n", timer.Local().String()) fmt.Printf("time in utc zone: %s\n", timer.UTC().String()) fmt.Printf("time: %s\n", timer.String()) fmt.Printf("time formatted: %s\n", timer.Format("2006-01-02 15:04:05")
獲取當(dāng)前的年/月/日 時(shí):分:秒 納秒
// 獲取當(dāng)前的年/月/日 時(shí):分:秒 納秒 fmt.Printf("current year: %d\n", timer.Year()) fmt.Printf("current month: %d %s\n", timer.Month(), timer.Month()) //返回的Month對象 fmt.Printf("current day: %d\n", timer.Day()) fmt.Printf("current hour: %d\n", timer.Hour()) fmt.Printf("current minute: %d\n", timer.Minute()) fmt.Printf("current second: %d\n", timer.Second()) fmt.Printf("current nanosecond: %d\n", timer.Nanosecond())
獲取當(dāng)前時(shí)間/日期
// 獲取當(dāng)前時(shí)間/日期 curHour, curMinute, curSecond := timer.Clock() fmt.Printf("current clock: %d:%02d:%02d\n", curHour, curMinute, curSecond) curYear, curMonth, curDay := timer.Date() fmt.Printf("current date: %d-%02d-%02d\n", curYear, curMonth, curDay)
編輯時(shí)間/求兩個(gè)日期的時(shí)間差
time.ParseDuration(durationString string)可以方便我們使用語義構(gòu)建時(shí)間跨度值,數(shù)值單位為納秒,比如:
timeDuration, _ := time.ParseDuration("24h")
timeDuration, _ := time.ParseDuration("12m")
timeDuration, _ := time.ParseDuration("6s")
timeDuration, _ := time.ParseDuration("1ms")
timeDuration, _ := time.ParseDuration("1us")
timeDuration, _ := time.ParseDuration("1ns")
// 已當(dāng)前時(shí)間為基增長年/月/日后的時(shí)間對象 timerAdded := timer.AddDate(1, 2, 3) curYear, curMonth, curDay = timerAdded.Date() fmt.Printf("current date: %d-%02d-%02d\n", curYear, curMonth, curDay) // 以當(dāng)前時(shí)間為基增長N納秒后的時(shí)間對象 比如增長了一天 timeDuration, _ := time.ParseDuration("24h") timerAdded = timer.Add(timeDuration) // 計(jì)算兩個(gè)時(shí)間的差值 返回的是納秒 按需求自行計(jì)算其他單位 // Duration is type of int64 and nanoseconds timeDuration = timerAdded.Sub(timer) fmt.Printf("days duration between %s~%s: %d\n", timerAdded.Format(timeLayout), timer.Format(timeLayout), timeDuration/1000/1000/1000/24/60/60)
使用 時(shí)間字符串 / Unix Timestamp 構(gòu)建時(shí)間對象
// 使用時(shí)間串獲取時(shí)間對象 timer, _ = time.Parse(timeLayout, "2018-08-08 08:08:08") // 使用時(shí)間串獲取時(shí)間對象 并設(shè)定時(shí)區(qū) timer, _ = time.ParseInLocation(timeLayout, "2018-08-08 08:08:08", timeLocation) // 使用Unix時(shí)間戳構(gòu)建時(shí)間對象 timer = time.Unix(1552368806, 0) //2019-03-12 13:33:26的Unix時(shí)間戳 fmt.Println(timer.Format(timeLayout))
獲取當(dāng)前時(shí)間是本年第幾天 本周第幾天
注意周日 的 Weekday編號為 0
// 獲取當(dāng)前時(shí)間是本年第幾天 本周第幾天 fmt.Printf("year day: %d, week day: %d\n", timer.YearDay(), timer.Weekday())
使用表征字符串轉(zhuǎn)換時(shí)間跨度
// 使用表征字符串轉(zhuǎn)換時(shí)間跨度 timeDuration, _ = time.ParseDuration("300s") fmt.Printf("nanosecond: %d\n", timeDuration) timeDuration, _ = time.ParseDuration("300us") fmt.Printf("nanosecond: %d\n", timeDuration)
以上就是golang如何設(shè)置時(shí)間的詳細(xì)內(nèi)容,更多請關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!
當(dāng)前題目:golang中設(shè)置時(shí)間的基本用法
文章分享:http://chinadenli.net/article24/ihjice.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站建設(shè)、、ChatGPT、網(wǎng)站維護(hù)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)