Golang time.Time.UTC(), Unix() and UnixNano() functions example

package time

Golang time.Time.UTC(), Unix() and UnixNano() functions usage example.

 package main

 import (
 "fmt"
 "time"
 )

 func main() {

 now := time.Now()

 fmt.Println("Today : ", now.Format("Mon, Jan 2, 2006 at 3:04pm"))

 utcTime := now.UTC()

 fmt.Println("Today in UTC : ", utcTime)

 unixTime := now.Unix()

 fmt.Println("Today in Unix time : ", unixTime)

 unixTimeNano := now.UnixNano()

 fmt.Println("Nanosecond in Unix time : ", unixTimeNano)

 // see also https://socketloop.com/tutorials/golang-convert-unix-timestamp-to-utc-timestamp
 }

Sample output :

Today : Mon, Aug 10, 2015 at 12:31pm

Today in UTC : 2015-08-10 04:31:51.901152485 +0000 UTC

Today in Unix time : 1439181111

Nanosecond in Unix time : 1439181111901152485

References :

http://golang.org/pkg/time/#Time.UTC

http://golang.org/pkg/time/#Time.Unix

http://golang.org/pkg/time/#Time.UnixNano

Advertisement