GoLang之标准库time包

本文基于Windos系统上Go SDK v1.8进行讲解

1.Now函数

func Now() Time {}

2.Time结构体

2.1Time结构体

type Time struct{}

2.2Year、 Month、Day、Hour、Minute、Second方法

func (t Time) Year() int {}
func (t Time) Month() Month {}
func (t Time) Day() int {}
func (t Time) Hour() int {}
func (t Time) Minute() int {}
func (t Time) Second() int {}

2.3Weekday方法

func (t Time) Weekday() Weekday {}

2.4Add方法

func (t Time) Add(d Duration) Time {}

2.5Sub方法

2.6Equal方法

2.7Before方法

2.8After方法

2.9Unix方法

func (t Time) Unix() int64 {}

2.10UnixNanon方法

func (t Time) UnixNano() int64 {}

2.11Format方法

func (t Time) Format(layout string) string {}
func main() {
	t := time.Now()
	fmt.Println(t) //2022-05-30 15:36:34.7520221 +0800 CST m=+0.012002901
	res := t.Format("2006-01-02 15:04:05")
	fmt.Println(res) //2022-05-30 15:36:34
	res = t.Format("2006/01/02")
	fmt.Println(res) //2022/05/30
	res = t.Format("15:04:05")
	fmt.Println(res) //15:36:34
}

3.UTC变量/Local变量

TC 标准时间是以 GMT(Greenwic加粗样式h Mean Time,格林尼治时间)这个时区为主,所以本地时间与 UTC 时间的时差就是本地时间与 GMT 时间的时差

4.Duration类型、Duration类型常量

type Duration int64    //Duration的单位其实可以是const变量中的所有

Millisecond //毫秒
Microsecond//微秒
Nanosecond//纳秒
const (
	Nanosecond  Duration = 1
	Microsecond          = 1000 * Nanosecond
	Millisecond          = 1000 * Microsecond
	Second               = 1000 * Millisecond
	Minute               = 60 * Second
	Hour                 = 60 * Minute
)

注:是不能单独打印的

5.Sleep函数

6.Weekday类型

type Weekday int

7.Unix函数

func Unix(sec int64, nsec int64) Time {} //sec是秒,nsec是纳秒