日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Golang時間及時間戳的獲取轉換超全面詳細講解_Golang

作者:Dorgan ? 更新時間: 2023-01-17 編程語言

獲取時間

獲取當前時間

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

欄目分類
最近更新