How to parse unix timestamp to time.Time

后端 未结 6 2037
悲&欢浪女
悲&欢浪女 2020-12-02 05:27

I\'m trying to parse an Unix timestamp but I get out of range error. That doesn\'t really makes sense to me, because the layout is correct (as in the Go docs):



        
6条回答
  •  [愿得一人]
    2020-12-02 06:29

    Sharing a few functions which I created for dates:

    Please note that I wanted to get time for a particular location (not just UTC time). If you want UTC time, just remove loc variable and .In(loc) function call.

    func GetTimeStamp() string {
         loc, _ := time.LoadLocation("America/Los_Angeles")
         t := time.Now().In(loc)
         return t.Format("20060102150405")
    }
    func GetTodaysDate() string {
        loc, _ := time.LoadLocation("America/Los_Angeles")
        current_time := time.Now().In(loc)
        return current_time.Format("2006-01-02")
    }
    
    func GetTodaysDateTime() string {
        loc, _ := time.LoadLocation("America/Los_Angeles")
        current_time := time.Now().In(loc)
        return current_time.Format("2006-01-02 15:04:05")
    }
    
    func GetTodaysDateTimeFormatted() string {
        loc, _ := time.LoadLocation("America/Los_Angeles")
        current_time := time.Now().In(loc)
        return current_time.Format("Jan 2, 2006 at 3:04 PM")
    }
    
    func GetTimeStampFromDate(dtformat string) string {
        form := "Jan 2, 2006 at 3:04 PM"
        t2, _ := time.Parse(form, dtformat)
        return t2.Format("20060102150405")
    }
    

提交回复
热议问题