Sling Academy
Home/Golang/How to convert UNIX timestamp to time in Go

How to convert UNIX timestamp to time in Go

Last updated: November 26, 2024

Working with timestamps in programming can be crucial for many applications, and Go (often referred to as Golang) provides a way to handle UNIX timestamps and convert them into a readable date format. This article will walk you through the process of converting a UNIX timestamp to a human-readable time in Go.

Understanding UNIX Timestamps

Before jumping into code, it’s important to understand what a UNIX timestamp is. It is the number of seconds that have elapsed since the Unix epoch, starting from 00:00:00 UTC on 1 January 1970 (not counting leap seconds).

Converting UNIX Timestamp to Time in Go

Go's time package provides handy methods for UNIX timestamp conversion. The time.Unix() method is particularly useful as it converts a timestamp into a time.Time object.

Step-by-Step Conversion

Let's look at the step-by-step process with code examples.

Basic Conversion

Here is a simple example of converting a UNIX timestamp to a formatted date string:


package main

import (
    "fmt"
    "time"
)

func main() {
    // Example UNIX timestamp
    unixTimestamp := int64(1633072800)
    
    // Convert to time.Time
    t := time.Unix(unixTimestamp, 0)
    
    // Format time.Time to string
    formattedTime := t.Format(time.RFC1123)
    fmt.Println("Converted time:", formattedTime)
}

In this example, we have:

  • Imported the necessary packages fmt and time.
  • Defined a unixTimestamp.
  • Used time.Unix() to convert the timestamp to a time.Time object.
  • Formatted the time using t.Format() with a pre-defined layout time.RFC1123.

Using Different Formats

Go allows you to format your time with various predefined layouts or create custom formats. Here's an example of using a custom format:


package main

import (
    "fmt"
    "time"
)

func main() {
    unixTimestamp := int64(1633072800)
    t := time.Unix(unixTimestamp, 0)
    
    customFormat := "Monday, 02-Jan-06 15:04:05 MST"
    formattedTime := t.Format(customFormat)
    fmt.Println("Custom formatted time:", formattedTime)
}

In this snippet:

  • We used a custom format, which reads Monday, 02-Jan-06 15:04:05 MST.
  • This format outputs a string with the full day name, the two-digit day of the month, abbreviated month, year, time, and timezone.

Conclusion

Converting UNIX timestamps to human-readable dates in Go is straightforward with the time package. Whether you need a standard format or a custom one, Go offers robust solutions for your timestamp conversion needs.

By utilizing the time.Unix() method in Go, you can easily manage and display time in your development projects with minimal lines of code.

Next Article: Go: How to get a slice of dates between 2 given dates

Previous Article: How to convert time to UNIX timestamp in Go

Series: Working with Date & Time in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant