網站首頁 編程語言 正文
一、獲取時間
1.1 獲取當前時間
代碼實現
package main import ( ? ? ? ? "fmt" ? ? ? ? "time" ) func main() { ? ?currentTime := time.Now() ? ? ? ? ? ? ? ? ?//當前時間 ? ?currentYear := time.Now().Year() ? ? ? ?//當前年 ? ?currentMonth := time.Now().Month() ? ? ? //當前月 ? ?currentDay := time.Now().Day() ? ? ? ? //當前日 ? ?currentHour := time.Now().Hour() ? ? ? ?//當前小時小時 ? ?currentMinute := time.Now().Minute() ? ? ?//當前分鐘 ? ?currentSecond := time.Now().Second() ? ? ?//當前秒 ? ?currentNSecond := time.Now().Nanosecond() ?//當前納秒 ? ? ? ?//打印結果 ? ?fmt.Println("當前時間是:", currentTime) ? ?fmt.Println("當前年:", currentYear) ? ?fmt.Println("當前月:", currentMonth) ? ?fmt.Println("當前日:", currentDay) ? ?fmt.Println("當前小時:", currentHour) ? ?fmt.Println("當前分鐘:", currentMinute) ? ?fmt.Println("當前秒:", currentSecond) ? ?fmt.Println("當前納秒:", currentNSecond) }
結果輸出
當前時間是: 2022-04-22 16:42:44.1160954 +0800 CST m=+0.004795301
當前年: 2022
當前月: April
當前日: 22
當前小時: 16
當前分鐘: 42
當前秒: 44
當前納秒: 150022700
1.2 獲取之前/之后的時間
獲取1分鐘之前的時間戳
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?currentTime := time.Now() ?? ?m, _ := time.ParseDuration("-1m") ?? ?result := currentTime.Add(m) ?? ?fmt.Println(result) }
說明
- time.ParseDuration 將傳入的"-1m"轉換為“持續時間”類型(time.Duration),輸出為-0h1m0s
- 之后Add() 可對其和時間進行計算
結果輸出
2022-04-22 16:43:20.9844622 +0800 CST m=-59.994691699
獲取一小時之前的時間
方法同上,持續時間可如下轉換:time.ParseDuration(“-1h”)
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?currentTime := time.Now() ?? ?m, _ := time.ParseDuration("-1h") ?? ?result := currentTime.Add(m) ?? ?fmt.Println(result) }
結果輸出
2022-04-22 15:47:01.9977133 +0800 CST m=-3599.992986699
獲取1小時后的時間
方法同上,持續時間可如下轉換:ime.ParseDuration(“1h”)
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?currentTime := time.Now() ?? ?m, _ := time.ParseDuration("1h") ?? ?result := currentTime.Add(m) ?? ?fmt.Println(result) }
結果輸出
2022-04-22 17:50:58.3395738 +0800 CST m=+3600.004424801
二、獲時間戳
2.1 獲取當前時間戳
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?fmt.Printf("時間戳(秒):%v;\n", time.Now().Unix()) ?? ?fmt.Printf("時間戳(納秒):%v;\n",time.Now().UnixNano()) ?? ?fmt.Printf("時間戳(毫秒):%v;\n",time.Now().UnixNano() / 1e6) ?? ?fmt.Printf("時間戳(納秒轉換為秒):%v;\n",time.Now().UnixNano() / 1e9) }
輸出結果
時間戳(秒):1650617834;
時間戳(納秒):1650617834110539400;
時間戳(毫秒):1650617834110;
時間戳(納秒轉換為秒):1650617834;
2.2 時間轉時間戳
代碼實現
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?timeStamp := time.Date(2022, 5, 20, 13, 14, 0, 0, time.Local).Unix() ?? ?fmt.Println("時間轉時間戳:",timeStamp) }
結果輸出
時間轉時間戳: 1653023640
示例:獲取當天01:00:00的時間戳
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?currentYear := time.Now().Year() ?? ?currentMonth := time.Now().Month() ?? ?currentDay := time.Now().Day() ?? ?timeStamp := time.Date(currentYear, currentMonth, currentDay, 1, 0, 0, 0, time.Local).Unix() ?? ?fmt.Println("當天某個時間點的時間戳:",timeStamp) }
結果輸出
當天某個時間點的時間戳: 1650560400
2.2 時間戳轉時間
基本使用
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?timeStr := time.Unix(1650617834, 0) ?? ?fmt.Println(timeStr) }
結果輸出
2022-04-22 16:57:14 +0800 CST
按模板格式化輸出
注意:模板格式里的時間不能隨意更改
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?timeLayout := "2006-01-02 15:04:05" ?? ?timeStr := time.Unix(1653023640, 0).Format(timeLayout) ?? ?fmt.Println(timeStr) }
結果輸出
2022-05-20 13:14:00
模板輸出當前時間
同上例,只是將當前時間戳轉換成了時間字串輸出
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?timeStamp := time.Now().Unix() ?? ?timeLayout := "2006-01-02 15:04:05" ?? ?timeStr := time.Unix(timeStamp, 0).Format(timeLayout) ?? ?fmt.Println(timeStr) }
結果輸出
2022-04-22 17:21:31
3. 時間計算
3.1 時間加時間段
方法
currentTime := time.Now() m, _ := time.ParseDuration("-1m") result := currentTime.Add(m)
示例
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?currentTime := time.Now() ?? ?m, _ := time.ParseDuration("-1m") ?? ?result := currentTime.Add(m) ?? ?fmt.Println(result) }
結果輸出
2022-04-22 17:23:04.2727584 +0800 CST m=-59.995884099
3.2 計算兩時間之差
語法
timeOne - timeTwo的方法如下:
timeOne.Sub(timeTwo)
示例
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?currentTime := time.Now() ?? ?//創建時間1 ?? ?timeDuOne, _ := time.ParseDuration("-1h") ?? ?timeOne := currentTime.Add(timeDuOne) ?? ?fmt.Println("時間1:",timeOne) ?? ?//創建時間2 ?? ?timeDuTwo, _ := time.ParseDuration("1h") ?? ?timeTwo := currentTime.Add(timeDuTwo) ?? ?fmt.Println("時間2:",timeTwo) ?? ?//計算兩時間 ?? ?dTime := timeOne.Sub(timeTwo) ?? ?fmt.Println("兩時間的差是", dTime) ?? ?m := timeOne.Sub(timeTwo) ?? ?fmt.Println("差值按分鐘計:", m.Minutes()) ?? ?h := timeOne.Sub(timeTwo) ?? ?fmt.Println("差值按小時計:", h.Hours()) ?? ?d := timeOne.Sub(timeTwo) ?? ?fmt.Println("差值按天計算:", d.Hours()/24) }
結果輸出
時間1: 2022-04-22 16:28:04.525285 +0800 CST m=-3599.994894399
時間2: 2022-04-22 18:28:04.525285 +0800 CST m=+3600.005105601
兩時間的差是 -2h0m0s
差值按分鐘計: -120
差值按小時計: -2
差值按天計算: -0.08333333333333333
原文鏈接:https://blog.csdn.net/cljdsc/article/details/124351164
相關推薦
- 2023-01-14 C++?win系統如何用MinGW編譯Boost庫_C 語言
- 2022-05-29 C語言?超詳細模擬實現單鏈表的基本操作建議收藏_C 語言
- 2022-07-12 CSS樣式:less語言的用法
- 2022-06-16 React實現核心Diff算法的示例代碼_React
- 2023-11-20 用python對數據進行擬合求函數表達式的方法
- 2023-10-30 Spring的BeanFactory與FactoryBean的區別
- 2022-10-11 CFS調度算法調度時機的理解
- 2023-03-29 Pytorch損失函數torch.nn.NLLLoss()的使用_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同步修改后的遠程分支