I have an integer
x := 1468540800
I want to fetch the date out of this unix timestamp in Golang. I have tried time.ParseDuration
but looks like that's not the correct way to extract date out of this. Converstion should happen like this http://www.unixtimestamp.com/index.php
I intend to convert into in ISO 8601 format may be. I want string like 2016-09-14
.
You may use t := time.Unix(int64(x), 0)
with location set to local time.
Or use t := time.Unix(int64(x), 0).UTC()
with the location set to UTC.
You may use t.Format("2006-01-02")
to format, Code (try on The Go Playground):
package main
import (
"fmt"
"time"
)
func main() {
x := 1468540800
t := time.Unix(int64(x), 0).UTC() //UTC returns t with the location set to UTC.
fmt.Println(t.Format("2006-01-02"))
}
output:
2016-07-15
Use time.Unix
with nanoseconds set to 0.
t := time.Unix(int64(x), 0)
Playground: https://play.golang.org/p/PpOv8Xm-CS.
$timestamp=1468540800;
echo gmdate("Y-m-d", $timestamp);
You can use strconv.ParseInt() for parsing to int64 in combination with time.Unix.
myTime,errOr := strconv.ParseInt(x, 10, 64)
if errOr != nil {
panic(errOr)
}
newTime := time.Unix(myTime, 0)