用习惯PHP的同学,用go语言如果想输出时间的话,可以参考如下示例:

package main
import (
"fmt"
"time"
)

func main() {
fmt.Println("time.Now():", time.Now())
fmt.Println("time.Now().Unix():", time.Now().Unix())
year:=time.Now().Year() //年
month:=time.Now().Month() //月
day:=time.Now().Day() //日
hour:=time.Now().Hour() //小时
minute:=time.Now().Minute() //分钟
second:=time.Now().Second() //秒
micro:=time.Now().Nanosecond()//纳秒
fmt.Println(year, month, day, hour, minute, second)
currentTimeData:=time.Date(year, month, day, hour, minute, second, micro, time.Local) //取当前时间,返回当前时间Time
fmt.Println("currentTimeData:", currentTimeData)
}

输出示例:

time.Now(): 2020-04-24 15:35:39.8018222 +0800 CST m=+0.006980601
time.Now().Unix(): 1587713739
2020 April 24 15 35 39
currentTimeData: 2020-04-24 15:35:39.830747 +0800 CST

如果需要实数的月份,用int(month)即可。

相关参考:https://golang.org/pkg/time/



Related Posts: go语言取当前时间 :

留言