網站首頁 編程語言 正文
1. 獲取時間
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) }
結果輸出
當前時間是: 2021-11-20 21:21:57.670992812 +0800 CST m=+0.000043774
當前年: 2021
當前月: November
當前日: 20
當前小時: 21
當前分鐘: 21
當前秒: 57
當前納秒: 671063850
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() 可對其和時間進行計算
輸出
2021-11-21 15:42:49.32800253 +0800 CST m=-59.999965656
獲取一小時之前的時間
方法同上,持續時間可如下轉換:
time.ParseDuration("-1h")
獲取1小時后的時間
方法同上,持續時間可如下轉換
ime.ParseDuration("1h")
2. 時間戳
2.1 獲取當前時間戳
package main import ( "fmt" "time" ) func main() { timeStamp := time.Now().Unix() //秒為單位的時間戳 timeStampN := time.Now().UnixNano() //納秒為單位的時間戳 fmt.Print(timeStamp,"\n",timeStampN,"\n") }
輸出
1637478374
1637478374774676883
2.2 時間 轉 時間戳
2.2.1 方法一
基本用法
package main import ( "fmt" "time" ) func main() { timeStamp := time.Date(2021, 11, 20, 23, 34, 10, 0, time.Local).Unix() fmt.Println(timeStamp) }
示例:獲取當天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) }
2.2.2 方法二
package main import ( "fmt" "time" ) func main() { timeLayout := "2006-01-02 15:04:05" timeStamp, _ := time.ParseInLocation(timeLayout, "2021-11-20 23:34:10", time.Local) fmt.Println(timeStamp) }
2.2 時間戳轉時間
基本使用
package main import ( "fmt" "time" ) func main() { timeStr := time.Unix(1637420154, 0) fmt.Println(timeStr) }
輸出
2021-11-20 22:55:54 +0800 CST
按模板格式化輸出
注意:模板格式里的時間不能隨意更改
package main import ( "fmt" "time" ) func main() { timeLayout := "2006-01-02 15:04:05" timeStr := time.Unix(1637420154, 0).Format(timeLayout) fmt.Println(timeStr) }
輸出
2021-11-20 22:55:54
模板輸出當前時間
同上例,只是將當前時間戳轉換成了時間字串輸出
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)
3. 時間計算
3.1 時間加時間段
方法
currentTime := time.Now() m, _ := time.ParseDuration("-1m") result := currentTime.Add(m)
示例
見 “1.2 獲取之前/之后的時間”
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: 2021-11-21 16:04:39.293524501 +0800 CST m=-3599.999909286
時間2: 2021-11-21 18:04:39.293524501 +0800 CST m=+3600.000090714
兩時間的差是 -2h0m0s
差值按分鐘計: -120
差值按小時計: -2
差值按天計算: -0.08333333333333333
總結
原文鏈接:https://blog.csdn.net/xingzuo_1840/article/details/121523643
相關推薦
- 2022-10-25 Python條件語句的使用_python
- 2023-07-16 oracle 創建存儲過程
- 2022-11-17 C語言數據結構不掛科指南之棧&隊列&數組詳解_C 語言
- 2022-10-21 golang?一次性定時器Timer用法及實現原理詳解_Golang
- 2022-08-07 Go?gRPC教程實現Simple?RPC_Golang
- 2022-11-15 簡單實現Android應用的啟動頁_Android
- 2023-04-09 使用Pytest.main()運行時參數不生效問題解決_python
- 2023-12-11 注解開發Mybatis
- 最近更新
-
- 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同步修改后的遠程分支