Bootstrap

Go- 时间与日期

Go 学习笔记,学习内容

主要介绍以下内容:

  • 时间的不同格式

  • 时间的格式化输出

代码示例可以直接运行

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now()
	fmt.Println(t)                     // 输出:2021-08-22 15:17:52.2654141 +0800 CST m=+0.002647001
	fmt.Println(t.UTC())               // 输出:2021-08-22 07:21:50.2108845 +0000 UTC
	fmt.Println(t.Format(time.RFC822)) // 输出:22 Aug 21 15:23 CST
	fmt.Println(t.Format(time.ANSIC))  // 输出:Sun Aug 22 15:23:53 2021

	fmt.Println(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) // 输出:2021 August 22 15 25 45
	fmt.Printf("%04d.%02d.%02d\n", t.Year(), t.Month(), t.Day())                // 输出:2021.08.22
}