代码实现
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
package main
import (
"fmt"
"time"
)
func main() {
currentTime := time.Now()
m, _ := time.ParseDuration("-1m")
result := currentTime.Add(m)
fmt.Println(result)
}
说明
结果输出
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
方法同上,持续时间可如下转换: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() {
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
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
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
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