sandeepkru 0e2f76a313 Update Epoch example to add UnixMilli from Time package
Standard library time package has support for UnixMilli - https://pkg.go.dev/time#Time.UnixMilli
Updated the example to use the method on Time instead of calculating.
2022-01-03 16:46:26 -08:00

30 lines
736 B
Go

// A common requirement in programs is getting the number
// of seconds, milliseconds, or nanoseconds since the
// [Unix epoch](http://en.wikipedia.org/wiki/Unix_time).
// Here's how to do it in Go.
package main
import (
"fmt"
"time"
)
func main() {
// Use `time.Now` with `Unix` or `UnixMilli` or `UnixNano`
// to get elapsed time since the Unix epoch in seconds or
// milliseconds or nanoseconds, respectively.
now := time.Now()
fmt.Println(now)
fmt.Println(now.Unix())
fmt.Println(now.UnixMilli())
fmt.Println(now.UnixNano())
// You can also convert integer seconds or nanoseconds
// since the epoch into the corresponding `time`.
fmt.Println(time.Unix(now.Unix(), 0))
fmt.Println(time.Unix(0, now.UnixNano()))
}