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
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")
方法同上,持续时间可如下转换
ime.ParseDuration("1h")
package main
import (
"fmt"
"time"
)
func main() {
timeStamp := time.Now().Unix() //秒为单位的时间戳
timeStampN := time.Now().UnixNano() //纳秒为单位的时间戳
fmt.Print(timeStamp,"\n",timeStampN,"\n")
}
输出
1637478374
1637478374774676883
package main
import (
"fmt"
"time"
)
func main() {
timeStamp := time.Date(2021, 11, 20, 23, 34, 10, 0, time.Local).Unix()
fmt.Println(timeStamp)
}
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)
}
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)
}
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)
currentTime := time.Now()
m, _ := time.ParseDuration("-1m")
result := currentTime.Add(m)
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