package main
import (
"fmt"
"reflect"
"time"
)
func main() {
now := time.Now()
fmt.Println("now = ", now)
fmt.Println("type of now = ", reflect.TypeOf(now))
year := now.Year()
fmt.Println("year = ", year)
month := now.Month()
fmt.Println("month = ", month, "(", int(month), ")")
day := now.Day()
fmt.Println("day = ", day)
hour := now.Hour()
minute := now.Minute()
second := now.Second()
nanosecond := now.Nanosecond()
yearday := now.YearDay()
fmt.Println("hour = ", hour)
fmt.Println("minute = ", minute)
fmt.Println("second = ", second)
fmt.Println("nanosecond = ", nanosecond)
fmt.Println("yearday = ", yearday)
weekday := now.Weekday()
fmt.Println("weekday = ", weekday, "(", int(weekday), ")")
y, M, d := now.Date()
fmt.Println("y = ", y, " m = ", int(M), " d = ", d)
h, m, s := now.Clock()
fmt.Println("h = ", h, " m = ", int(m), " s = ", s)
fmt.Println("Local = ", now.Local())
fmt.Println("UTC = ", now.UTC())
fmt.Println("location = ", now.Location())
name, offset := now.Zone()
fmt.Println("name = ", name, " offset = ", offset)
fmt.Println("unix time = ", now.Unix())
}
编译输出:
C:/Go/bin/go.exe build -i [D:/golang/src/Mychann]
成功: 进程退出代码 0.
D:/golang/src/Mychann/Mychann.exe [D:/golang/src/Mychann]
now = 2018-02-08 15:47:49.5971309 +0800 CST m=+0.006000300
type of now = time.Time
year = 2018
month = February ( 2 )
day = 8
hour = 15
minute = 47
second = 49
nanosecond = 597130900
yearday = 39
weekday = Thursday ( 4 )
y = 2018 m = 2 d = 8
h = 15 m = 47 s = 49
Local = 2018-02-08 15:47:49.5971309 +0800 CST
UTC = 2018-02-08 07:47:49.5971309 +0000 UTC
location = Local
name = CST offset = 28800
unix time = 1518076069
成功: 进程退出代码 0.