drvvvuyia15070493
drvvvuyia15070493
2016-09-14 14:07
浏览 145
已采纳

如何提取Unix时间戳并获取日期

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duandao7704
    duandao7704 2016-09-14 14:21
    已采纳

    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
    
    点赞 评论
  • dsy6639
    dsy6639 2016-09-14 14:11

    Use time.Unix with nanoseconds set to 0.

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

    Playground: https://play.golang.org/p/PpOv8Xm-CS.

    点赞 评论
  • duandai7601
    duandai7601 2016-09-14 14:11
    $timestamp=1468540800;
    echo gmdate("Y-m-d", $timestamp);
    
    点赞 评论
  • dpjmljx90897323
    dpjmljx90897323 2016-09-14 14:12

    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)
    
    点赞 评论

相关推荐