How to parse a milliseconds-since-epoch timestamp string in Go?
2021 update: you can use UnixMilli
to parse from the integer. This would improve the accepted answer to:
func msToTime(ms string) (time.Time, error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.UnixMilli(msInt), nil
}
The format string does not support milliseconds since the epoch, so you need to parse manually. For example:
func msToTime(ms string) (time.Time, error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, msInt*int64(time.Millisecond)), nil
}
Check out http://play.golang.org/p/M1dWGLT8XE to play with the example live.