Sling Academy
Home/Golang/How to format date and time in Go

How to format date and time in Go

Last updated: November 26, 2024

Working with date and time is a common task in many applications. Go, also known as Golang, provides powerful tools for formatting date and time using the time package. In this article, we will explore how to format date and time values in Go and provide several code snippets to help you understand the process.

Understanding Go's Time Format

Go uses a unique approach to date and time formatting. Instead of using common placeholders like DD for day or MM for month, Go uses a reference date and time - Mon Jan 2 15:04:05 2006 MST - to represent the different components of date and time. Each part of this memorable string corresponds to a part of the date and time.

  • 1 stands for the day
  • Jan stands for the month
  • 15 stands for the hour in 24-hour time
  • 04 stands for the minute
  • 05 stands for the second
  • 2006 stands for the year
  • MST stands for the time zone

Formatting Date and Time Strings

To format dates and times in Go, we use the Format method from the time.Time type.

package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    formattedTime := currentTime.Format("02-Jan-2006 03:04:05 PM")
    fmt.Println("Formatted Time:", formattedTime)
}

In the example above, the current time is formatted as 02-Jan-2006 03:04:05 PM, which means the day is followed by the abbreviated month name, full year, and the time in 12-hour notation followed by AM or PM.

Using Predefined Formats

Go provides several predefined format constants for common layouts:

  • time.ANSIC - Mon Jan _2 15:04:05 2006
  • time.UnixDate - Mon Jan _2 15:04:05 MST 2006
  • time.RFC1123 - Mon, 02 Jan 2006 15:04:05 MST
package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    fmt.Println("ANSIC:", currentTime.Format(time.ANSIC))
    fmt.Println("UnixDate:", currentTime.Format(time.UnixDate))
    fmt.Println("RFC1123:", currentTime.Format(time.RFC1123))
}

The predefined formats simplify the process for commonly used formats by allowing you to use easy constants instead of building them manually.

Customizing Time Zone and Locale

You can adjust the time zone or locale by using the LoadLocation method or parse your date and time strings to localize them appropriately:

package main

import (
    "fmt"
    "time"
)

func main() {
    loc, err := time.LoadLocation("Europe/Madrid")
    if err != nil {
        fmt.Println(err)
        return
    }

    currentTime := time.Now().In(loc)
    fmt.Println("Time in Madrid:", currentTime.Format("02-Jan-2006 15:04:05"))
}

With the LoadLocation function, you can convert the current time to a specific time zone by passing the appropriate location string, as shown in the example for Madrid's time zone.

Conclusion

Formatting date and time in Go offers a lot of flexibility and powerful predefined constants. Whether you need to display date and time in a specific format or convert it across various time zones, Go's time package provides the tools necessary to do it efficiently.

Next Article: How to subtract 2 dates in Go

Previous Article: How to get the current date and time 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