Sling Academy
Home/Golang/How to convert local time to UTC time and vice versa in Go

How to convert local time to UTC time and vice versa in Go

Last updated: November 26, 2024

When working with time data in programming, it's often necessary to convert between local time and UTC (Coordinated Universal Time). The Go programming language offers comprehensive support for time operations via its time package. In this article, I'll guide you through the process of converting local time to UTC and vice versa using Go.

Converting Local Time to UTC

To convert local time to UTC, you need to obtain the local time first, then use the UTC() method provided by the time.Time type. Here's how you can do it:

package main

import (
    "fmt"
    "time"
)

func main() {
    localTime := time.Now()
    fmt.Println("Local Time:", localTime)

    utcTime := localTime.UTC()
    fmt.Println("UTC Time:", utcTime)
}

In this snippet, time.Now() returns the current local time, and UTC() converts it to UTC.

Converting UTC Time to Local Time

To convert a UTC time to local time, you can use the In() method, along with the time.Local location constant, which represents the local time. The following example demonstrates this conversion:

package main

import (
    "fmt"
    "time"
)

func main() {
    // Simulate a UTC time by parsing a known UTC datetime string
    utcLayout := "2006-01-02 15:04:05"
    utcTimeStr := "2023-09-08 14:00:00"
    utcTime, err := time.Parse(utcLayout, utcTimeStr)
    if err != nil {
        fmt.Println("Error parsing UTC time:", err)
        return
    }

    fmt.Println("UTC Time:", utcTime)

    localTime := utcTime.In(time.Local)
    fmt.Println("Local Time:", localTime)
}

Here, we parse a known UTC date and time string into a time.Time object and then use In(time.Local) to convert this UTC time
to the local time according to the system’s time zone settings.

Considerations

  • Format: The time parsing format must match the input string format precisely. Use the reference time "2006-01-02 15:04:05" for parsing.
  • Time Zones: The local time functions use the host system's settings for time zones. Ensure your system clock and settings are correct to avoid unexpected results.
  • Daylight Saving Time: Local time conversion automatically handles daylight saving time changes based on the system's regional settings.

Be careful with assumptions about time zones and daylight saving time rules, as legislation changes can affect them. Regularly update and test your application in varying environments.

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

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