Go (Golang) Unix Timestamp Guide

Complete guide to working with Unix timestamps in Go. Learn how to get current epoch time, convert dates to timestamps, and convert timestamps to readable dates using Go's time package.

Go provides excellent support for Unix timestamps through the time package. Go uses a unique reference time (Mon Jan 2 15:04:05 MST 2006) for layout parsing, which can be remembered as "01/02 03:04:05PM '06 -0700". This guide covers all essential timestamp operations in Go.

How to get the current epoch time

Go
time.Now().Unix()
go

More Go

Convert from human-readable date to epoch

Go
Example code
go

See Go documentation

Convert from epoch to human-readable date

Go
Example code
go

See Go documentation

Additional Go Examples

Getting Current Timestamp

package main

import (
    "fmt"
    "time"
)

func main() {
    // Get current Unix timestamp in seconds
    now := time.Now()
    unixSeconds := now.Unix()
    fmt.Println(unixSeconds)
    
    // Get current Unix timestamp in milliseconds
    unixMillis := now.UnixMilli()
    fmt.Println(unixMillis)
    
    // Get current Unix timestamp in nanoseconds
    unixNanos := now.UnixNano()
    fmt.Println(unixNanos)
}
go

Go provides methods for seconds, milliseconds, and nanoseconds precision.

Converting Timestamp to Time

package main

import (
    "fmt"
    "time"
)

func main() {
    // Convert Unix timestamp (seconds) to time.Time
    unixSeconds := int64(1609459200)
    t := time.Unix(unixSeconds, 0)
    fmt.Println(t)
    
    // Convert Unix timestamp (milliseconds) to time.Time
    unixMillis := int64(1609459200000)
    t = time.UnixMilli(unixMillis)
    fmt.Println(t)
    
    // Format time
    formatted := t.Format("2006-01-02 15:04:05")
    fmt.Println(formatted)
}
go

Use time.Unix() or time.UnixMilli() to convert timestamps back to time.Time.

Go's Unique Layout Parsing

package main

import (
    "fmt"
    "time"
)

func main() {
    // Parse date string using Go's reference time layout
    // Reference time: Mon Jan 2 15:04:05 MST 2006
    // Layout: "2006-01-02 15:04:05"
    dateString := "2024-01-01 12:00:00"
    layout := "2006-01-02 15:04:05"
    
    t, err := time.Parse(layout, dateString)
    if err != nil {
        panic(err)
    }
    
    // Convert to Unix timestamp
    unixSeconds := t.Unix()
    fmt.Println(unixSeconds)
    
    // Parse with timezone
    t, err = time.Parse("2006-01-02 15:04:05 MST", "2024-01-01 12:00:00 UTC")
    if err != nil {
        panic(err)
    }
    fmt.Println(t.Unix())
}
go

Go uses a reference time (Mon Jan 2 15:04:05 MST 2006) for layout strings. Remember: "01/02 03:04:05PM '06 -0700".

About Go Unix Timestamps

Go provides excellent support for Unix timestamps through the time package. Go uses a unique reference time (Mon Jan 2 15:04:05 MST 2006) for layout parsing, which can be remembered as "01/02 03:04:05PM '06 -0700". This guide covers all essential timestamp operations in Go, including getting current timestamps, converting between timestamps and dates, and working with Go's unique date formatting system.

Related guides: Check out our guides for other programming languages: JavaScript, Python, Java, and more. For timestamp conversion tools, visit our Tools page.

Frequently Asked Questions

How do I get the current Unix timestamp in Go?

Use time.Now().Unix() to get the current timestamp in seconds, or time.Now().UnixMilli() for milliseconds. The time package provides excellent support for Unix timestamps in Go.

How do I convert a Unix timestamp to a date in Go?

Use time.Unix(seconds, nanoseconds) to create a Time value from a Unix timestamp. For timestamps in seconds, use time.Unix(timestamp, 0). You can then format it using Format() method.

What is Go's reference time for layout parsing?

Go uses a unique reference time "Mon Jan 2 15:04:05 MST 2006" for layout strings, which can be remembered as "01/02 03:04:05PM '06 -0700". This is different from other languages and is a key concept in Go date formatting.

Related Guides