如何提取unix时间戳和获取日期



我有一个整数

x := 1468540800

我想在Golang中获取这个unix时间戳的日期。我已经尝试过time.ParseDuration,但看起来这不是从中提取日期的正确方法。对话应该是这样进行的http://www.unixtimestamp.com/index.php
我打算转换成在ISO 8601格式可能是。我想要像2016-09-14这样的字符串

您可以使用t := time.Unix(int64(x), 0)并将位置设置为本地时间。
或者使用位置设置为UTC的t := time.Unix(int64(x), 0).UTC()

您可以使用t.Format("2006-01-02")来格式化,代码(在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"))
}
输出:

2016-07-15

使用time.Unix,将纳秒设置为0。

t := time.Unix(int64(x), 0)

游乐场:https://play.golang.org/p/PpOv8Xm-CS。

可以结合time.Unix使用strconv.ParseInt()解析为int64。

myTime,errOr := strconv.ParseInt(x, 10, 64)
    if errOr != nil {
        panic(errOr)
    }
    newTime := time.Unix(myTime, 0)
$timestamp=1468540800;
echo gmdate("Y-m-d", $timestamp);

最新更新