網站首頁 編程語言 正文
獲取時間
獲取當前時間
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分鐘之前的時間戳
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
獲時間戳
獲取當前時間戳
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;
時間轉時間戳
package main import ( "fmt" "time" ) func main() { timeStamp := time.Date(2022, 5, 20, 13, 14, 0, 0, time.Local).Unix() fmt.Println("時間轉時間戳:",timeStamp) }
結果輸出
時間轉時間戳: 1653023640
時間戳轉時間
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
題外話:獲取當前時間的年月日時分秒的拼接(月日等補足到兩位)
now := time.Now()
nowSecond := strconv.Itoa(now.Year()) + fmt.Sprintf("%02d", now.Month()) + fmt.Sprintf("%02d", now.Day()) + fmt.Sprintf("%02d", now.Hour()) + fmt.Sprintf("%02d", now.Minute()) + fmt.Sprintf("%02d", now.Second())
結果輸出
20221209103544
原文鏈接:https://blog.csdn.net/Dorgan/article/details/128248547
相關推薦
- 2022-03-24 基于Docker的可持續交付問題_docker
- 2022-08-04 Go?slice切片make生成append追加copy復制示例_Golang
- 2022-04-28 pytorch中torch.topk()函數的快速理解_python
- 2022-03-26 C語言中指針常量和常量指針的區別_C 語言
- 2022-04-05 C語言用遞歸函數實現漢諾塔_C 語言
- 2023-10-25 解決使用window.open()或window.location.href跳轉后返回/后退原頁面不
- 2022-12-03 PostgreSQL?數組類型操作使用及特點詳解_PostgreSQL
- 2022-08-05 linux安裝部署lua環境
- 最近更新
-
- 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同步修改后的遠程分支