⌚ Unix time in Go [cheatsheet]
The time package in Go contains convenient functions for operating on Unix time (also known as Epoch time, Posix time, seconds since the Epoch, or UNIX Epoch time). Using them, you can easily get the current Unix timestamp, convert a time.Time struct to Unix time, and vice versa - Unix timestamp to time.Time. Check out this cheatsheet to learn how to do it and come back to it whenever you forget 😉
Convert date/time to Unix epoch time #
The time.Time struct contains Unix(), UnixMilli(), UnixMicro() and UnixNano() methods for converting a time object to a Unix timestamp of type int64.
date := time.Date(2022, 6, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(date.Unix())
fmt.Println(date.UnixMilli())
fmt.Println(date.UnixMicro())
fmt.Println(date.UnixNano())
Output:
1654041600
1654041600000
1654041600000000
1654041600000000000
Get current Unix timestamp #
To get the current Unix timestamp in Go, you just need to get the current time of type time.Time using the time.Now() function and convert it to Unix timestamp using Unix(), UnixMilli(), UnixMicro(), or UnixNano() method.
Get current Unix/Epoch time in seconds #
fmt.Println(time.Now().Unix())
Output:
1657861095
Get current Unix/Epoch time in milliseconds #
fmt.Println(time.Now().UnixMilli())
Output:
1657861095093
Get current Unix/Epoch time in microseconds #
fmt.Println(time.Now().UnixMicro())
Output:
1657861095093115
Get current Unix/Epoch time in nanoseconds #
fmt.Println(time.Now().UnixNano())
Output:
1657861095093117000
Convert Unix timestamp to time.Time #
The time package has three functions to convert from an int64 Unix timestamp to a time.Time object:
Unix(sec int64, nsec int64) Timeto convert a seconds timestamp, a nanoseconds timestamp, or timestamp with a seconds and nanoseconds part to atime.Timeobject.UnixMilli(msec int64) Timeto convert from a millisecond timestamp to atime.Time.UnixMicro(usec int64) Timeto convert a microsecond timestamp to atime.Timestruct.
Second Unix timestamp to time.Time #
var timestamp int64 = 1657861095
t := time.Unix(timestamp, 0)
fmt.Println(t.UTC())
Output:
2022-07-15 04:58:15 +0000 UTC
Millisecond Unix timestamp to time.Time #
var timestamp int64 = 1657861095093
t := time.UnixMilli(timestamp)
fmt.Println(t.UTC())
Output:
2022-07-15 04:58:15.093 +0000 UTC
Microsecond Unix timestamp to time.Time #
var timestamp int64 = 1657861095093115
t := time.UnixMicro(timestamp)
fmt.Println(t.UTC())
Output:
2022-07-15 04:58:15.093115 +0000 UTC
Nanosecond Unix timestamp to time.Time #
var timestamp int64 = 1657861095093117123
t := time.Unix(0, timestamp)
fmt.Println(t.UTC())
Output:
2022-07-15 04:58:15.093117123 +0000 UTC
Parse Unix timestamp from string to time.Time #
To parse a string Unix timestamp and get a time.Time object, you must first convert the timestamp from string to int64, and then convert it to time.Time using the Unix(), UnixMilli(), or UnixMicro() function.
timeString := "1657861095"
timestamp, err := strconv.ParseInt(timeString, 10, 64)
if err != nil {
panic(err)
}
t := time.Unix(timestamp, 0)
fmt.Println(t.UTC())
Output:
2022-07-15 04:58:15 +0000 UTC