Introduction
Working with dates and times is a common requirement in many applications. Go’s time
package provides comprehensive support for parsing and formatting dates and times. In this chapter, you will learn the basics of formatting and parsing time in Go, including examples of common formats and usage patterns.
Formatting Time
The time.Time
type has a Format
method that allows you to convert a time.Time
object to a string. Go uses a unique reference date, Mon Jan 2 15:04:05 MST 2006
, to specify the layout for formatting and parsing. Each part of this reference date represents a component of the date and time format.
Example: Formatting Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Default format:", now)
fmt.Println("Custom format 1:", now.Format("2006-01-02 15:04:05"))
fmt.Println("Custom format 2:", now.Format("02-Jan-2006 03:04:05 PM"))
fmt.Println("Custom format 3:", now.Format("Mon Jan _2 15:04:05 2006"))
fmt.Println("Custom format 4:", now.Format("2006-01-02"))
fmt.Println("Custom format 5:", now.Format("15:04:05"))
}
In this example, the Format
method is used to convert the current time to different string formats based on the provided layout.
Parsing Time
The time.Parse
function allows you to convert a string to a time.Time
object using a specified layout. The layout must match the format of the input string.
Example: Parsing Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02 15:04:05"
str := "2023-06-18 14:30:00"
t, err := time.Parse(layout, str)
if err != nil {
fmt.Println("Error parsing time:", err)
return
}
fmt.Println("Parsed time:", t)
}
In this example, the time.Parse
function is used to convert a string to a time.Time
object using the specified layout.
Parsing Time in a Specific Location
If the input string does not include time zone information, you can use time.ParseInLocation
to parse the time in a specific location.
Example: Parsing Time in a Specific Location
Example:
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02 15:04:05"
str := "2023-06-18 14:30:00"
location, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println("Error loading location:", err)
return
}
t, err := time.ParseInLocation(layout, str, location)
if err != nil {
fmt.Println("Error parsing time:", err)
return
}
fmt.Println("Parsed time in location:", t)
}
In this example, the time.ParseInLocation
function is used to parse a time string in the "America/New_York" time zone.
Formatting and Parsing with time.RFC3339
Go provides several predefined layouts for common date and time formats, such as time.RFC3339
.
Example: Using time.RFC3339
Example:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// Formatting using RFC3339
formatted := now.Format(time.RFC3339)
fmt.Println("RFC3339 format:", formatted)
// Parsing using RFC3339
str := "2023-06-18T14:30:00Z"
t, err := time.Parse(time.RFC3339, str)
if err != nil {
fmt.Println("Error parsing time:", err)
return
}
fmt.Println("Parsed RFC3339 time:", t)
}
In this example, the time.RFC3339
layout is used for both formatting and parsing.
Formatting and Parsing with Custom Layouts
You can define your custom layouts to match the specific format you need.
Example: Custom Layouts
Example:
package main
import (
"fmt"
"time"
)
func main() {
layout := "02-Jan-2006 03:04:05 PM"
now := time.Now()
// Formatting with custom layout
formatted := now.Format(layout)
fmt.Println("Custom format:", formatted)
// Parsing with custom layout
str := "18-Jun-2023 02:30:00 PM"
t, err := time.Parse(layout, str)
if err != nil {
fmt.Println("Error parsing time:", err)
return
}
fmt.Println("Parsed custom time:", t)
}
In this example, a custom layout is defined for both formatting and parsing a date string.
Handling Time Zones
Go provides functions to handle time zones, such as converting between time zones and getting the local time.
Example: Handling Time Zones
Example:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// Load a specific time zone
location, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println("Error loading location:", err)
return
}
// Convert to the specified time zone
newYorkTime := now.In(location)
fmt.Println("New York time:", newYorkTime)
// Get the local time zone
localTime := now.Local()
fmt.Println("Local time:", localTime)
// Get the UTC time
utcTime := now.UTC()
fmt.Println("UTC time:", utcTime)
}
In this example, the current time is converted to different time zones and formats.
Conclusion
Go’s time
package provides powerful and flexible tools for formatting and parsing dates and times. By understanding how to use the Format
and Parse
methods with various layouts, you can handle a wide range of date and time operations in your Go applications. Whether you are working with standard formats like RFC3339
or custom layouts, Go’s time
package has the functionality you need to manage dates and times effectively.