Quick Answers: Go Timestamp
- time.Now().Unix() gives Unix seconds. UnixMilli() and UnixNano() give the same instant in finer units.
- time.Unix(seconds, 0) builds a time. Call .UTC() on it for the UTC reading, or .Format(time.RFC3339) for the ISO string.
- time.Parse needs a layout. Use time.RFC3339 for ISO dates, and never ignore the error it returns.
- LoadLocation handles time zones properly. It reads the system database, so daylight saving follows the real rules.
Go Counts Seconds, Like Unix Does
Go counts time the same way Unix does: in seconds since the epoch, stored as an int64. time.Now().Unix() returns that count, time.Now().UnixMilli() returns milliseconds, and time.Now().UnixNano() returns nanoseconds, all from the same clock. That makes Go straightforward for timestamp work, because the value coming out of the standard library is already the value most databases and APIs expect. The part of Go that surprises newcomers is not the counting but the formatting, which uses a reference date instead of format codes.
The converter above shows the same numbers live, and it detects all four precisions from the digit count, so a UnixNano() value pasted in is recognised as nanoseconds at once instead of being read as a date far in the future.
Current Unix Timestamp in Go
package main
import (
"fmt"
"time"
)
func main() {
// Current Unix timestamp in seconds
now := time.Now().Unix()
fmt.Println(now)
// Milliseconds and nanoseconds
fmt.Println(time.Now().UnixMilli())
fmt.Println(time.Now().UnixNano())
}time.Now().Unix() is the standard way to read the clock in Go. It returns whole seconds, which is the unix timestamp, and it is the value to store in a database column or send in an API call. UnixMilli() and UnixNano() are for measuring short intervals, where the fractional seconds matter. For logging, the seconds value is the one to keep: it is 10 digits, it sorts as a number, and every other language on this site reads it the same way.
Unix Timestamp to Date in Go
package main
import (
"fmt"
"time"
)
func main() {
// Unix seconds to date
t := time.Unix(1715429912, 0).UTC()
fmt.Println(t) // 2024-05-11 12:18:32 +0000 UTC
// As RFC 3339
fmt.Println(t.Format(time.RFC3339))
// 2024-05-11T12:18:32Z
}The reference value used across this site is 1715429912, which is Saturday, 11 May 2024 at 12:18:32 UTC. time.Unix(1715429912, 0) builds that instant, and .UTC() pins it to UTC before formatting. Format with time.RFC3339 for the ISO reading 2024-05-11T12:18:32Z, or with a custom layout such as 2006-01-02 15:04:05. That layout is the reference date, Monday 2 January 2006 at 15:04:05, which Go uses instead of format codes: the numbers in the layout are the example, not the code.
Date to Unix Timestamp in Go
package main
import (
"fmt"
"time"
)
func main() {
// Date to Unix timestamp
t, err := time.Parse(time.RFC3339, "2024-05-11T12:18:32Z")
if err != nil {
panic(err)
}
fmt.Println(t.Unix()) // 1715429912
}time.Parse takes a layout and a string, and returns a time plus an error. time.RFC3339 is the layout for ISO 8601 strings such as 2024-05-11T12:18:32Z, and the Z in the string means UTC. The error matters: a string that does not match the layout gives the zero time, and the zero time converts to a large negative timestamp, so check the error before calling Unix(). The converter at the top of this page accepts the same ISO strings, so you can check a parsing result there before trusting it in code.
Go Timestamps in Other Time Zones
package main
import (
"fmt"
"time"
)
func main() {
t := time.Unix(1715429912, 0).UTC()
// The same instant in other zones
khi, _ := time.LoadLocation("Asia/Karachi")
fmt.Println(t.In(khi)) // 2024-05-11 17:18:32 +0500 PKT
ny, _ := time.LoadLocation("America/New_York")
fmt.Println(t.In(ny)) // 2024-05-11 08:18:32 -0400 EDT
}time.LoadLocation takes an IANA zone name such as Asia/Karachi and returns the location, and .In() converts a time into it. The reference timestamp reads as 17:18:32 in Karachi and 08:18:32 in New York, both the same instant, with the offset printed beside each one. LoadLocation reads the system time zone database, so daylight saving follows the real rules. The timezone converter on this site shows the same instant in 12 zones at once, so you can check a LoadLocation result against it in seconds.
Common Go Timestamp Mistakes
- Ignoring the Parse error. A string that does not match the layout gives the zero time. Check err before calling Unix().
- Using the wrong layout. time.RFC3339 expects a T and a zone designator. A string such as 2024-05-11 12:18:32 needs the layout 2006-01-02 15:04:05 instead.
- Forgetting .UTC(). time.Unix returns a time in UTC by default, but a time built other ways may carry Local. Call .UTC() when the reading must be UTC.
- Adding fixed hours for zones. New York is 5 hours behind UTC in winter and 4 in summer. Use LoadLocation and .In(), never arithmetic.
- Storing UnixNano() where seconds belong. A 19 digit nanosecond value in a seconds column reads as a date hundreds of thousands of years ahead. Use Unix() for storage.
Go Timestamp FAQs
How do I get the current unix timestamp in Go?
Use time.Now().Unix(). It returns the current time in seconds since the epoch, as an int64. If you need more precision, time.Now().UnixMilli() gives milliseconds and time.Now().UnixNano() gives nanoseconds. All three read the same clock, so they agree with each other and with the live converter above.
How do I convert a unix timestamp to a date in Go?
Use time.Unix with the seconds and zero nanoseconds, then UTC: time.Unix(1715429912, 0).UTC() gives 2024-05-11 12:18:32 +0000 UTC. Format it with .Format(time.RFC3339) for 2024-05-11T12:18:32Z, or with a custom layout such as 2006-01-02 15:04:05. The layout uses the reference date 2 January 2006, 15:04:05, which is the part of Go that surprises newcomers most.
How do I convert a date to a unix timestamp in Go?
Parse the date first, then call Unix(): t, _ := time.Parse(time.RFC3339, "2024-05-11T12:18:32Z"); t.Unix() gives 1715429912. time.Parse returns a time in UTC when the string carries a Z, and in the zone named by its offset otherwise. Never ignore the error it returns: a string that does not match the layout gives the zero time, and the zero time converts to a large negative timestamp.
Why does Go use a reference date for formatting?
Go formats dates with a layout built from the reference time Monday, 2 January 2006, 15:04:05, MST. The layout 2006-01-02 15:04:05 means year-month-day hour-minute-second, because those are the numbers in the reference date. It looks strange the first time, but it removes the ambiguity of format codes: there is no %Y or %m to memorise, only the example date itself. The constants time.RFC3339 and time.DateTime cover the common cases.
What is the difference between Unix(), UnixMilli() and UnixNano() in Go?
Only the unit. Unix() returns seconds, UnixMilli() returns milliseconds, and UnixNano() returns nanoseconds, all since the same epoch. The seconds value is 10 digits, the millisecond value is 13, and the nanosecond value is 19. Use Unix() for storing, comparing and sending, because that is the value other languages expect. Use UnixNano() only when you are measuring something that happens in less than a millisecond.
How do I handle time zones in Go?
Use time.LoadLocation with an IANA zone name, then call In() on the time: loc, _ := time.LoadLocation("Asia/Karachi"); t.In(loc) gives 2024-05-11 17:18:32 +0500 PKT for the reference timestamp. LoadLocation reads the system time zone database, so daylight saving follows the real rules. The local zone is time.Local, and UTC is time.UTC. Never add a fixed number of hours by hand.
Does Go handle the year 2038 problem?
Yes. Go times use int64 seconds, so the 32 bit limit does not apply. The timestamp 2147483647, which is 19 January 2038 at 03:14:07 UTC, is just an ordinary value in Go, and times far past it work the same way. The limit only bites when a Go value is stored in a 32 bit column or sent to a system that stores it that way, so check the storage type, not the language.
How do I check if a unix timestamp is expired in Go?
Compare it with the current Unix time: time.Now().Unix() > exp means the token or session is expired. Both sides are seconds since the epoch, so the comparison is plain integer arithmetic. That is the same check the JWT decoder on this site does for the exp claim. Make sure the value you compare is really in seconds: a millisecond value from JavaScript is a thousand times larger and makes everything look expired.
Related tools
Source: Go standard library, time package. Reference values on this page were checked against the Go documentation on 27 November 2026.