在开发的过程中,大家都会使用到golang中的time模块进行一些时间的处理,以及格式化下面我总结了一些time中常用的方法运行就可以使用
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now)
fmt.Println(now.Year())
fmt.Println(now.Month())
fmt.Println(now.Day())
fmt.Println(now.Hour())
fmt.Println(now.Minute())
fmt.Println(now.Second())
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)
fmt.Println(format2)
fmt.Println(format3)
fmt.Println(format4)
fmt.Println(format5)
fmt.Println(now.Unix())
fmt.Println(now.UnixMilli())
fmt.Println(now.UnixMicro())
fmt.Println(now.UnixNano())
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)
fmt.Println(time2)
fmt.Println(time3)
NewTime := now.Add(time.Hour * 1)
fmt.Println(NewTime)
SubTime := NewTime.Sub(now)
fmt.Println(SubTime)
fmt.Println(time.Since(NewTime))
date := time.Date(2022, 12, 05, 12, 30, 45, 0, time.UTC)
fmt.Println(now.Before(date))
fmt.Println(now.After(date))
}