網站首頁 編程語言 正文
文章目錄
- time 包
time 包
UTC or CST
utc 是世界協調時間(標準),cst 是中央標準時間
當UTC時間為0點時,中國CST時間要加 8h,即為8點AM
當UTC時間為0點時,美國CST時間要減 6h,即為18點PM
返回 Time 時間類型
// 返回當前時間 Time 類型
now := time.Now()
fmt.Printf("%v", now)
// 輸出
2022-05-29 19:47:46.435119 +0800 CST m=+0.000401431
// 返回指定時間的 Time 類型 UTC 時間
date := time.Date(2022, 1, 2, 3, 4, 5, 0, time.UTC)
fmt.Printf("%v", date)
// 輸出
2022-01-02 03:04:05 +0000 UTC
拿到 Time 中年月日時分秒類型
// 返回具體年月日時分秒
date := time.Date(2022, 1, 2, 3, 4, 5, 0, time.UTC)
fmt.Printf("%v,%v,%v,%v,%v,%v", date.Year(), date.Month(), date.Day(), date.Hour(), date.Minute(), date.Second())
// 輸出
2022,January,2,3,4,5
格式化 Time 類型輸出
// 格式化 Time 類型輸出,返回 string
date := time.Date(2022, 1, 2, 3, 4, 5, 0, time.UTC)
fmt.Println(date.Format("2006-1-2 15:04:05"))
// 輸出
2022-1-2 03:04:05
求兩個 Time 時間差
// 求兩個 Time 時差 Duration
date1 := time.Date(2022, 1, 2, 3, 4, 5, 0, time.UTC)
date2 := time.Date(2022, 1, 2, 4, 5, 6, 0, time.UTC)
diff := date2.Sub(date1)
fmt.Printf("%v", diff)
// 輸出
1h1m1s
// diff.Hours() 或者 diff.Minutes() 或者 diff.Seconds() 可以把這個 Duration 完全轉成時或分或秒
把時間字符串解析成一種格式
// 把時間 string 解析成一種格式,返回 Time, error
date, _ := time.Parse("2006-01-02 15:04:05", "2022-01-02 03:04:05")
fmt.Printf("%v", date)
// 輸出
2022-01-02 03:04:05 +0000 UTC
unix時間戳
// 當前時間 Time 類型轉 int64 時間戳秒級
now := time.Now()
ts := now.Unix()
nts := now.UnixNano()
fmt.Printf("%v", ts)
fmt.Printf("%v", nts)
// 輸出
1653827591
1653827591000000000
// 時間戳秒級 轉 Time。Unix 函數第一個參數是 sec,第二個參數 nsec
date := time.Unix(1653827353, 0)
fmt.Printf("%v", date)
// 輸出
2022-05-29 20:29:13 +0800 CST
// 時間戳秒級 轉 Time。1s = 1e3 ms,1ms = 1e6 ns
date := time.Unix(0, 1653827353e9)
fmt.Printf("%v", date)
// 輸出
2022-05-29 20:29:13 +0800 CST
原文鏈接:https://abcnull.blog.csdn.net/article/details/125036130
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-10-20 C語言手寫集合List的示例代碼_C 語言
- 2022-10-10 VMware?Workstation與Device/Credential?Guard不兼容的解決_V
- 2023-05-09 Python實現刪除重復文件的示例代碼_python
- 2022-06-13 ASP.NET?Core?MVC路由(Routing)的用法_基礎應用
- 2022-12-05 關于EF的Code?First的使用以及踩坑記錄_實用技巧
- 2023-07-04 注冊npm上傳包報This operation requires a one-time passwo
- 2021-12-09 數據庫建表設計六范式介紹_數據庫其它
- 2023-01-03 python案例中Flask全局配置示例詳解_python
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支