一般程序会有获取 Unix 时间 的秒数,毫秒数,或者微秒数的需求。来看看如何用 Go 来实现。 | |
![]() ![]() package main | |
import ( "fmt" "time" ) | |
func main() { | |
分别使用 | now := time.Now() secs := now.Unix() nanos := now.UnixNano() fmt.Println(now) |
注意 | millis := nanos / 1000000 fmt.Println(secs) fmt.Println(millis) fmt.Println(nanos) |
你也可以将 Unix 纪元起的整数秒或者纳秒转化到相应的时间。 | fmt.Println(time.Unix(secs, 0)) fmt.Println(time.Unix(0, nanos)) } |
$ go run epoch.go 2012-10-31 16:13:58.292387 +0000 UTC 1351700038 1351700038292 1351700038292387000 2012-10-31 16:13:58 +0000 UTC 2012-10-31 16:13:58.292387 +0000 UTC | |
下面我们将看看另一个时间相关的任务:时间解析与格式化。 |
下一个例子: 时间的格式化和解析