In daily development, we often encounter the needs of global applications such as cross-border e-commerce and international conference scheduling. At this time, dealing with multi-time zone time is a common problem. For example, you may need to convert Beijing time for an event to Los Angeles time, New York time, and London time. This article will explain in detail how to do this task gracefully using Go.
Go official time package introduction
Go provides a powerful time processing package time, which includes the core capabilities of processing time, time zone and date conversion.
All Abouttime
The usage method of the package can be found in the official documentationtime packageFound in
When performing multi-time zone time conversion, understanding the following core concepts is crucial for operating time:
- : The time in Go is represented by a structure, including date, time, time zone and other information.
- : Time zone information, Go uses to represent the area of time.
- and : Used to parse and format time strings.
How to convert Beijing time to other time zone time
Suppose we need to convert the current Beijing time to Los Angeles time, New York time and London time. The following steps are required:
- Get Beijing time.
- Load the target time zone.
- Use the method to convert Beijing time to the target time zone.
Convert current Beijing time to Los Angeles, New York and London time
package main import ( "fmt" "time" ) func main() { // Step 1: Define the time zone where Beijing time is located beijingLocation, err := ("Asia/Shanghai") if err != nil { ("The time zone where Beijing time is located cannot be loaded:", err) return } // Step 2: Get the current Beijing time now := () beijingTime := (beijingLocation) ("Beijing time:", ("2006-01-02 15:04:05")) // Step 3: Loading the target time zone losAngelesLocation, err := ("America/Los_Angeles") if err != nil { ("Unable to load the time zone where Los Angeles time is:", err) return } newYorkLocation, err := ("America/New_York") if err != nil { ("Unable to load the time zone where New York time is:", err) return } londonLocation, err := ("Europe/London") if err != nil { ("Cannot load the time zone where London time is:", err) return } // Step 4: Time converted to different time zones losAngelesTime := (losAngelesLocation) newYorkTime := (newYorkLocation) londonTime := (londonLocation) // Step 5: Print the result ("Los Angeles Time:", ("2006-01-02 15:04:05")) ("New York Time:", ("2006-01-02 15:04:05")) ("London Time:", ("2006-01-02 15:04:05")) }
Convert the designated Beijing time to Los Angeles, New York and London time
package main import ( "fmt" "time" ) func main() { // Beijing time string beijingTimeStr := "2025-01-15 10:00:00" // Define the time format layout := "2006-01-02 15:04:05" // parse the string as beijingLocation, _ := ("Asia/Shanghai") beijingTime, err := (layout, beijingTimeStr, beijingLocation) if err != nil { ("Time parsing error:", err) return } // Convert to Los Angeles time losAngelesLocation, _ := ("America/Los_Angeles") losAngelesTime := (losAngelesLocation) // Convert to New York time newYorkLocation, _ := ("America/New_York") newYorkTime := (newYorkLocation) // Convert to London time londonLocation, _ := ("Europe/London") londonTime := (londonLocation) ("Beijing time:", (layout)) ("Los Angeles Time:", (layout)) ("New York Time:", (layout)) ("London Time:", (layout)) }
Verify that the conversion time is correct
EnterTime, date, time zone and time difference around the world now - 24 hours, search separatelyBeijing time、Los Angeles Time、New York timeandLondon time. Compare the results and compare the daylight saving time information if there is a demand for daylight saving time.
Things to note
- Correct loading of time zone data
Go uses the IANA time zone database (TZ database). Time zone names such as "Asia/Shanghai" and "America/Los_Angeles" are standard time zone identifiers.
Make sure that the operating environment (especially the container environment) contains the latest time zone data, otherwise the time zone information may be inaccurate. - Daylight saving time treatment
Daylight saving time (DST) may be enabled in different regions. Go's time zone data automatically handles daylight saving time without manual intervention.
For example, Los Angeles will be 1 hour faster than Standard Time during daylight saving time.
This is the article about the example code for multi-time zone time conversion in Go. For more related content on multi-time zone time conversion in Go, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!