欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Golang中如何使用日期/時間包

Golang中如何使用日期/時間包,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

城中網(wǎng)站建設公司創(chuàng)新互聯(lián)建站,城中網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為城中上千提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的城中做網(wǎng)站的公司定做!

golang 的日期時間包:time 的使用方式。

  • time package 包含了 time.Time 時間對象 及 構建此時間對象的一些方法(time.Unix(), time.Parse())

  • golang 可精確到 nanosecond,故相應的函數(shù)返回值或參數(shù)都已納秒為單位,我們可以借助time.ParseDuration(durationString string)友好的生成納秒度量的時間跨度值

  • golang 的時間格式串Layout固定為 2006-01-02 15:04:05

  • golang 默認使用 Local 即本地時區(qū),可以通過 time.LoadLocation(zoneName string) (*Location, error)來設定時區(qū)

時區(qū)構建/格式化模式串

// 構建時區(qū)
var timeLocation *time.Location
timeLocation, _ = time.LoadLocation("")       //UTC
timeLocation, _ = time.LoadLocation("UTC")      //UTC
timeLocation, _ = time.LoadLocation("Local")     //Local
timeLocation, _ = time.LoadLocation("Asia/Shanghai") //使用時區(qū)碼

//golang的時間格式化pattern
var timeLayout = "2006-01-02 15:04:05"

當前時間對象

// 獲取當前時間對象
var timer time.Time
timer = time.Now()

// 為時間設定時區(qū) 可以通過 timer.Local()/timer.UTC() 快速設定時區(qū)
timer.In(timeLocation)

獲取秒級時間戳/納秒級時間戳

// 獲取當前秒級時間戳
var curTimestamp int64
curTimestamp = timer.Unix()
println("current timestamp:" + strconv.FormatInt(curTimestamp, 10))

// 獲取當前納秒及時間戳 1秒=1000毫秒=1000,000微妙=1000,000,000納秒
var curNanoTimestamp int64
curNanoTimestamp = timer.UnixNano()
println("current nano timestamp:" + strconv.FormatInt(curNanoTimestamp, 10))

獲取本地時間的時區(qū)/CST標準時間/自定義格式

// 獲取本地時間的時區(qū)/快速獲取時區(qū)時間/自定義格式
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"))

獲取當前的年/月/日 時:分:秒 納秒

// 獲取當前的年/月/日 時:分:秒 納秒
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())

獲取當前時間/日期

// 獲取當前時間/日期
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)

編輯時間/求兩個日期的時間差

time.ParseDuration(durationString string)可以方便我們使用語義構建時間跨度值,數(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")

// 已當前時間為基增長年/月/日后的時間對象
timerAdded := timer.AddDate(1, 2, 3)
curYear, curMonth, curDay = timerAdded.Date()
fmt.Printf("current date: %d-%02d-%02d\n", curYear, curMonth, curDay)

// 以當前時間為基增長N納秒后的時間對象 比如增長了一天
timeDuration, _ := time.ParseDuration("24h")
timerAdded = timer.Add(timeDuration)
// 計算兩個時間的差值 返回的是納秒 按需求自行計算其他單位
// 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)

使用 時間字符串 / Unix Timestamp 構建時間對象

// 使用時間串獲取時間對象
timer, _ = time.Parse(timeLayout, "2018-08-08 08:08:08")
// 使用時間串獲取時間對象 并設定時區(qū)
timer, _ = time.ParseInLocation(timeLayout, "2018-08-08 08:08:08", timeLocation)
// 使用Unix時間戳構建時間對象
timer = time.Unix(1552368806, 0) //2019-03-12 13:33:26的Unix時間戳
fmt.Println(timer.Format(timeLayout))

獲取當前時間是本年第幾天 本周第幾天

注意周日 的 Weekday編號為 0

// 獲取當前時間是本年第幾天 本周第幾天
fmt.Printf("year day: %d, week day: %d\n", timer.YearDay(), timer.Weekday())

使用表征字符串轉(zhuǎn)換時間跨度

// 使用表征字符串轉(zhuǎn)換時間跨度
timeDuration, _ = time.ParseDuration("300s")
fmt.Printf("nanosecond: %d\n", timeDuration)
timeDuration, _ = time.ParseDuration("300us")
fmt.Printf("nanosecond: %d\n", timeDuration)

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

分享名稱:Golang中如何使用日期/時間包
本文來源:http://aaarwkj.com/article18/iidhdp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、自適應網(wǎng)站、虛擬主機、網(wǎng)站制作靜態(tài)網(wǎng)站、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設
亚洲激情视频在线视频| 国产精品毛片在线看不卡| 国产亚洲精品国产福利久久| 蜜臀av午夜福利在线观看| 亚洲老熟女老妇老女人| 亚洲少妇熟女一区二区三区| 久久综合午夜福利视频| 免费看男人添女人无遮挡| 亚洲国产日韩精品久久| 日韩一区二区三区av在线| 精品国产一区=区三区乱码| 国产一区二区欧美日本| av电影网站中文字幕| 欧美一区二区三区亚洲| 亚洲av天堂免费在线观看| 免费看欧美粗又大爽老| 美国一级二级三级黄片| 日韩视频免费看一区二区| 国产黄色一区二区三区四区| 爱爱网爱综合日日干夜夜操| av在线视频男人的天堂| 国产日产亚洲综合一区| 三级久久三级久久三级| 日本一区二区日本一区| 欧美大片免费在线播放| 日韩成人高清免费在线| 国产亚洲一区二区高清| 国产a天堂一区二区专区| 午夜福利激情视频在线| 亚洲国产精品自拍第一页| 日本午夜精品在线观看| 日本国产一区二区在线观看| 国产精品日本欧美久久久| 日本在线视频精品一区| 日韩传媒在线观看视频| 国产黄片一区二区在线| 亚洲成人av在线播放观看| 亚洲国产中文字幕高清| 亚洲精品成人午夜久久| 日日摸夜夜添添出白浆| 99精品人妻一区二区三区|