前言
Gotime
Now():获取当前本地的时间
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now) // 2022-12-03 21:06:16.1658077 +0800 CST m=+5.936223001
}
Now()timeTime
获取具体时间单位的值(yeah、month、day ······)
Now()Time
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("年:", now.Year())
fmt.Println("月:", now.Month())
fmt.Println("数字格式的月:", int(now.Month()))
fmt.Println("日:", now.Day())
fmt.Println("时:", now.Hour())
fmt.Println("分:", now.Minute())
fmt.Println("秒:", now.Second())
}
TimeYear()Month()Day()Hour()Minute()Second()
时间格式化
TimeFormat(layout string)string
import (
"fmt"
"time"
)
func main() {
now := time.Now()
format1 := now.Format("2006-01-02 15:04:05")
format2 := now.Format("2006/01/02 15:04:05")
format3 := now.Format("2006-01-02")
format4 := now.Format("2006/01/02")
format5 := now.Format("15:04:05")
fmt.Println(format1) // 2022-12-03 22:27:56
fmt.Println(format2) // 2022/12/03 22:27:56
fmt.Println(format3) // 2022-12-03
fmt.Println(format4) // 2022/12/03
fmt.Println(format5) // 22:27:56
}
layout Go2006-01-02 15:04:05-
获取秒、微秒、毫秒、纳秒
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// 获取秒
fmt.Println(now.Unix()) // 1670078476
// 获取毫秒
fmt.Println(now.UnixMilli()) // 1670079987508082
// 获取微秒
fmt.Println(now.UnixMicro()) // 1670079987508082
// 获取纳秒
fmt.Println(now.UnixNano()) // 1670079987508082500
}
timeUnix()UnixMilli()UnixMicro()UnixNano()
通过指定年月日等参数获取时间
import (
"fmt"
"time"
)
func main() {
date := time.Date(2002, 12, 03, 12, 12, 12, 0, time.UTC)
fmt.Println(date) // 2022-12-03 12:12:12 +0000 UTC
}
Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
时间戳与时间的转换
import (
"fmt"
"time"
)
func main() {
now := time.Now()
time1 := time.Unix(now.Unix(), 0).Format("2006-01-02 15:04:05")
time2 := time.UnixMilli(now.UnixMilli()).Format("2006-01-02 15:04:05")
time3 := time.UnixMicro(now.UnixMicro()).Format("2006-01-02 15:04:05")
fmt.Println(time1) // 2022-12-03 23:03:33
fmt.Println(time2) // 2022-12-03 23:03:33
fmt.Println(time3) // 2022-12-03 23:03:33
}
Unix()UnixMilli()UnixMicro()
字符串转时间格式
import (
"fmt"
"time"
)
func main() {
t1, err := time.Parse("2006-01-02 15:04:05", "2022-12-03 13:00:00")
if err != nil {
fmt.Println("err: ", err)
return
}
fmt.Println(t1) // 2022-12-03 13:00:00 +0000 UTC
t2, err := time.Parse("2006-01-02", "2022-12-03")
if err != nil {
fmt.Println("err: ", err)
return
}
fmt.Println(t2) // 2022-12-03 00:00:00 +0000 UTC
t3, err := time.Parse("15:04:05", "13:00:00")
if err != nil {
fmt.Println("err: ", err)
return
}
fmt.Println(t3) // 0000-01-01 13:00:00 +0000 UTC
}
Parse(layout, value string) (Time, error)timelayoutvalueerror
时间的添加和减少操作
import (
"fmt"
"time"
)
func main() {
now := time.Now()
newTime := now.Add(time.Hour * 1)
fmt.Println(newTime.Format("2006-01-02 15:04:05"))
}
(t Time) Add(d Duration) TimeDurationtime.Hourtime.Minutetime.Second
计算两个时间的时间差
import (
"fmt"
"time"
)
func main() {
now := time.Now()
newTime := now.Add(time.Hour * 1)
fmt.Println(newTime.Sub(now)) // 1h0m0s
}
Sub(u Time) Duration
计算当前时间与某个时间的时间差
import (
"fmt"
"time"
)
func main() {
beforeTime := time.Now().Add(time.Hour * -1)
fmt.Println(time.Since(beforeTime)) // 1h0m0s
}
Add(d Duration) TimeSince(t Time) Duration
判断当前时间是否在某个时间之前
import (
"fmt"
"time"
)
func main() {
now := time.Now()
date := time.Date(2022, 12, 03, 12, 12, 12, 0, time.UTC)
fmt.Println(now.Before(date)) // false
}
Before(u Time) booltruefalse
判断当前时间是否在某个时间之后
import (
"fmt"
"time"
)
func main() {
now := time.Now()
date := time.Date(2022, 12, 03, 12, 12, 12, 0, time.UTC)
fmt.Println(now.After(date)) // true
}
After(u Time) booltruefalse
小结
本文介绍了如果获取当前时间、在当前时间的前提下获取具体的年月日时分秒、时间格式化和时间戳与时间的转换以及计算时间差的方法等。掌握了这些函数和方法的使用,应对开发中 时间操作的场景不成问题。