網站首頁 編程語言 正文
獲取當前時間的年、月、日、時、分、秒的方法如下:
// 獲取當前時間 now := time.Now() // 當前時間的年、月、日、小時、分鐘、秒和納秒都可以通過現有接口直接獲取 year := now.Year() month := now.Month() day := now.Day() hour := now.Hour() minute := now.Minute() second := now.Second() nanosecond := now.Nanosecond() // 當前時間的微秒和毫秒是通過納秒計算生成 microsecond := nanosecond / 1e3 millisecond := nanosecond / 1e6 fmt.Println(now.Format("2006-01-02 15:04:05.000000000")) fmt.Println(year, month, day, hour, minute, second, nanosecond, microsecond, millisecond)
運行結果如下:
# 當前時間格式輸出
2022-06-09 19:25:52.022598620
2022 June 9 19 25 52 22598620 22598 22
獲取從1970到現在經過的時間的方法如下:
// 獲取從1970經過的時間,秒和納秒都可以通過現有接口直接獲取 sec := now.Unix() // 時間戳位數為10 ms := now.UnixMilli() // 時間戳位數為13 us := now.UnixMicro() // 時間戳位數為16 ns := now.UnixNano() // 時間戳位數為19 fmt.Printf("sec:%v\n ms:%v\n us:%v\n ns:%v\n", sec, ms, us, ns)
運行結果如下:
# 1970經過的時間格式輸出
sec:1654773952
?ms:1654773952022
?us:1654773952022598
?ns:1654773952022598620
時間間隔格式化輸出方法:
// 時間間隔返回的是time.Duration,下面以1h1m1s1ms1us1ns的時間間隔舉例,測試各種格式的打印效果 duration := 1*time.Hour + 1*time.Minute + 1*time.Second + 1*time.Millisecond + 1*time.Microsecond + 1*time.Nanosecond // 直接使用%v打印,不轉換sec、ms或其他。 fmt.Printf("duration:%v\n", duration) fmt.Printf("duration:%6v\n", duration) fmt.Printf("duration:%.6v\n", duration) fmt.Printf("duration:%.3v\n", duration) // duration支持Hours()、 Minutes()、Seconds() 和 // Milliseconds()、Microseconds()、Nanoseconds()接口 // 前三個接口返回類型為float64可以通過0.3f打印小數點后的數, // 后三個為int64,是整數,小數點后都是0 // 下面列舉秒和毫秒的格式打印,其他時間單位可以參考秒和毫秒 // 秒的打印格式%f可以打印小數點后9位,精確到納秒 fmt.Printf("duration:%vsec\n", duration.Seconds()) fmt.Printf("duration:%0.3fsec\n", duration.Seconds()) fmt.Printf("duration:%0.6fsec\n", duration.Seconds()) // 毫秒沒有小數點,都是整數,轉換成float后,小數點后都是0 fmt.Printf("duration:%vms\n", duration.Milliseconds()) fmt.Printf("duration:%.3dms\n", duration.Milliseconds()) fmt.Printf("duration:%.3fms\n", float64(duration.Milliseconds())) }
行結果如下:
# 1h1m1s1ms1us1ns的時間間隔舉例格式輸出
# %v沒有單位轉換的時間輸出
duration:1h1m1.001001001s
duration:1h1m1.001001001s
duration:1h1m1.
duration:1h1# 秒的格式輸出
duration:3661.001001001sec
duration:3661.001sec
duration:3661.001001sec# 毫秒的格式輸出
duration:3661001ms
duration:3661001ms
duration:3661001.000ms
通過測試程序可以看到:
1.沒時間單位轉換的格式輸出,直接用%v能精確到ns,%.3V,只是對輸出的字符串進行了切割。此處建議直接用%v即可。
2.對于秒的格式輸出,%v精確到小數點9位,即納秒。當然可以根據%f的格式調整,例如%.3f精確到毫秒
3.對于毫秒的格式輸出,直接用%v或%d即可,轉換成float64沒有意義
一般在統計一個函數或一段程序運行了多長時間,一般建議使用第二種方式,轉換成秒的格式輸出,再根據精度調整%f的格式即可。
第一種方式有可能出現時間單位不統一,例如一個是分鐘,一個是秒。
上面例子完成的代碼如下:
package main import ( "fmt" "time" ) func main() { // 獲取當前時間 now := time.Now() // 當前時間的年、月、日、小時、分鐘、秒和納秒都可以通過現有接口直接獲取 year := now.Year() month := now.Month() day := now.Day() hour := now.Hour() minute := now.Minute() second := now.Second() nanosecond := now.Nanosecond() // 當前時間的微秒和毫秒是通過納秒計算生成 microsecond := nanosecond / 1e3 millisecond := nanosecond / 1e6 fmt.Println(now.Format("2006-01-02 15:04:05.000000000")) fmt.Println(year, month, day, hour, minute, second, nanosecond, microsecond, millisecond) // 獲取從1970經過的時間,秒和納秒都可以通過現有接口直接獲取 sec := now.Unix() // 時間戳位數為10 ms := now.UnixMilli() // 時間戳位數為13 us := now.UnixMicro() // 時間戳位數為16 ns := now.UnixNano() // 時間戳位數為19 fmt.Printf("sec:%v\n ms:%v\n us:%v\n ns:%v\n", sec, ms, us, ns) // 時間間隔返回的是time.Duration,下面以1h1m1s1ms1us1ns的時間間隔舉例,測試各種格式的打印效果 duration := 1*time.Hour + 1*time.Minute + 1*time.Second + 1*time.Millisecond + 1*time.Microsecond + 1*time.Nanosecond // 直接使用%v打印,不轉換sec、ms或其他。 fmt.Printf("duration:%v\n", duration) fmt.Printf("duration:%6v\n", duration) fmt.Printf("duration:%.6v\n", duration) fmt.Printf("duration:%.3v\n", duration) // duration支持Hours()、 Minutes()、Seconds() 和 // Milliseconds()、Microseconds()、Nanoseconds()接口 // 前三個接口返回類型為float64可以通過0.3f打印小數點后的數, // 后三個為int64,是整數,小數點后都是0 // 下面列舉秒和毫秒的格式打印,其他時間單位可以參考秒和毫秒 // 秒的打印格式%f可以打印小數點后9位,精確到納秒 fmt.Printf("duration:%vsec\n", duration.Seconds()) fmt.Printf("duration:%0.3fsec\n", duration.Seconds()) fmt.Printf("duration:%0.6fsec\n", duration.Seconds()) // 毫秒沒有小數點,都是整數,轉換成float后,小數點后都是0 fmt.Printf("duration:%vms\n", duration.Milliseconds()) fmt.Printf("duration:%.3dms\n", duration.Milliseconds()) fmt.Printf("duration:%.3fms\n", float64(duration.Milliseconds())) }
下面是時間間隔的時間單位轉換的源碼:
// time.go // Nanoseconds returns the duration as an integer nanosecond count. func (d Duration) Nanoseconds() int64 { return int64(d) } // Microseconds returns the duration as an integer microsecond count. func (d Duration) Microseconds() int64 { return int64(d) / 1e3 } // Milliseconds returns the duration as an integer millisecond count. func (d Duration) Milliseconds() int64 { return int64(d) / 1e6 } // These methods return float64 because the dominant // use case is for printing a floating point number like 1.5s, and // a truncation to integer would make them not useful in those cases. // Splitting the integer and fraction ourselves guarantees that // converting the returned float64 to an integer rounds the same // way that a pure integer conversion would have, even in cases // where, say, float64(d.Nanoseconds())/1e9 would have rounded // differently. // Seconds returns the duration as a floating point number of seconds. func (d Duration) Seconds() float64 { sec := d / Second nsec := d % Second return float64(sec) + float64(nsec)/1e9 } // Minutes returns the duration as a floating point number of minutes. func (d Duration) Minutes() float64 { min := d / Minute nsec := d % Minute return float64(min) + float64(nsec)/(60*1e9) } // Hours returns the duration as a floating point number of hours. func (d Duration) Hours() float64 { hour := d / Hour nsec := d % Hour return float64(hour) + float64(nsec)/(60*60*1e9) }
補充:如果想格式化為12小時方式,需指定PM
。
func formatDemo() { now := time.Now() // 格式化的模板為Go的出生時間2006年1月2號15點04分 Mon Jan // 24小時制 fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan")) // 12小時制 fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan")) fmt.Println(now.Format("2006/01/02 15:04")) fmt.Println(now.Format("15:04 2006/01/02")) fmt.Println(now.Format("2006/01/02")) }
總結
原文鏈接:https://blog.csdn.net/SaberJYang/article/details/125209495
相關推薦
- 2022-11-02 用戶態和內核態-用戶線程和內核態線程的區別_其它相關
- 2022-11-22 Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解_Android
- 2022-07-19 在 NgModule 里通過依賴注入的方式注冊服務實例
- 2023-01-15 PyQt5+QtChart實現繪制曲線圖_python
- 2023-07-28 el-tabs 切換之前判斷是否滿足條件,不滿足則提示用戶,不能切換
- 2024-01-10 給idea添加右鍵打開功能
- 2022-10-29 vite 配置 @ 路徑別名
- 2022-02-10 postgresql解決鎖表
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支