How to Parse Unix Timestamp to time.Time in Go
Created
Modified
Using time.Unix Function
You can directly use time.Unix function of time which converts the unix time stamp to UTC.
See the following example:
package main
import (
"fmt"
"time"
)
func main() {
// sec seconds and nsec nanoseconds since January 1, 1970 UTC.
var i int64 = 1651383433
t := time.Unix(i, 0)
fmt.Println(t.UTC())
}
2022-05-01 05:37:13 +0000 UTC
Using milliseconds
The built-in time.Unix() function supports second and nanosecond precision. For example,
package main
import (
"fmt"
"time"
)
func main() {
var ms int64 = 1651383433905
t := time.Unix(ms/int64(1000), (ms%int64(1000))*int64(1000000))
fmt.Println(t.UTC())
}
2022-05-01 05:37:13.905 +0000 UTC