好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Golang 日期/時間包的使用詳解

Golang 日期/時間包的使用詳解

熱門標簽:學海導航地圖標注 南通如皋申請開通400電話 廣州呼叫中心外呼系統 中國地圖標注省會高清 西部云谷一期地圖標注 浙江高速公路地圖標注 地圖標注的汽車標 江西轉化率高的羿智云外呼系統 高德地圖標注口訣

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

  • time package 包含了 time.Time 時間對象 及 構建此時間對象的一些方法(time.Unix(), time.Parse())
  • golang 可精確到 nanosecond,故相應的函數返回值或參數都已納秒為單位,我們可以借助time.ParseDuration(durationString string)友好的生成納秒度量的時間跨度值
  • golang 的時間格式串Layout固定為 2006-01-02 15:04:05
  • golang 默認使用 Local 即本地時區,可以通過 time.LoadLocation(zoneName string) (*Location, error)來設定時區

時區構建/格式化模式串

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

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

當前時間對象

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

// 為時間設定時區 可以通過 timer.Local()/timer.UTC() 快速設定時區
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))

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

// 獲取本地時間的時區/快速獲取時區時間/自定義格式
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)可以方便我們使用語義構建時間跨度值,數值單位為納秒,比如:
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")
// 使用時間串獲取時間對象 并設定時區
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())

使用表征字符串轉換時間跨度

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • golang gorm中格式化時間問題詳解
  • golang時間、時區、格式的使用方法
  • Golang獲取當前時間代碼
  • golang 獲取明天零點的時間戳示例
  • 基于golang時間轉換的問題
  • 詳解Golang中的各種時間操作

標簽:許昌 德宏 貴州 曲靖 常州 保定 東營 吐魯番

巨人網絡通訊聲明:本文標題《Golang 日期/時間包的使用詳解》,本文關鍵詞  Golang,日期,時間,包,的,使用,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Golang 日期/時間包的使用詳解》相關的同類信息!
  • 本頁收集關于Golang 日期/時間包的使用詳解的相關信息資訊供網民參考!
  • 推薦文章