📌  相关文章
📜  golang string to time - Go 编程语言 - Go 编程语言(1)

📅  最后修改于: 2023-12-03 14:41:33.497000             🧑  作者: Mango

Golang String to Time

Golang is a popular programming language for creating web applications and APIs. One of the common tasks in web development is parsing date and time strings into Go's time.Time format. This guide will provide a comprehensive overview of how to convert a string to a time.Time object in Go.

Converting a String to a time.Time Object

Go's standard library provides several functions for parsing a string into a time.Time object. The most commonly used function is time.Parse().

package main

import (
	"fmt"
	"time"
)

func main() {
	dateString := "2021-08-30T08:30:00Z"
	layout := "2006-01-02T15:04:05Z"
	date, err := time.Parse(layout, dateString)
	if err != nil {
		fmt.Println("Error parsing date:", err)
		return
	}
	fmt.Println("Parsed date:", date)
}

In the code above, we are using the time.Parse() function to convert a string representation of a date, "2021-08-30T08:30:00Z", into a time.Time object. The second parameter to time.Parse() is a format layout string, which specifies the format of the input string. In our case, we are using the layout "2006-01-02T15:04:05Z", which represents the date and time in UTC format.

The time.Parse() function returns two values: the parsed time.Time object and an error. If the parsing is successful, the object is returned, and the error is nil. If there is an error during parsing, the error is returned, and the object is nil.

Common Date and Time Layouts

The layout string used in time.Parse() has a specific format that must match the input string. Below is a table of common date and time layouts and their corresponding format strings.

| Layout | Format | |--------|--------| | 01/02/2006 | 01/02/2006 | | 15:04:05 | 15:04:05 | | Mon Jan 2 15:04:05 MST 2006 | Mon Jan 2 15:04:05 MST 2006 | | 2006-01-02T15:04:05Z07:00 | 2006-01-02T15:04:05Z07:00 | | 2006-01-02T15:04:05.999999999Z07:00 | 2006-01-02T15:04:05.999999999Z07:00 |

In the code below, we are using the "01/02/2006" layout to parse a date string in the format "08/30/2021".

package main

import (
	"fmt"
	"time"
)

func main() {
	dateString := "08/30/2021"
	layout := "01/02/2006"
	date, err := time.Parse(layout, dateString)
	if err != nil {
		fmt.Println("Error parsing date:", err)
		return
	}
	fmt.Println("Parsed date:", date)
}
Time Zones

In Go, a time.Time object is based on the UTC time zone by default. You can use the time.LoadLocation() function to load a specific time zone and parse a string into that time zone.

package main

import (
	"fmt"
	"time"
)

func main() {
	dateString := "2021-08-30T08:30:00"
	layout := "2006-01-02T15:04:05"
	loc, err := time.LoadLocation("America/New_York")
	if err != nil {
		fmt.Println("Error loading location:", err)
		return
	}
	date, err := time.ParseInLocation(layout, dateString, loc)
	if err != nil {
		fmt.Println("Error parsing date:", err)
		return
	}
	fmt.Println("Parsed date:", date)
}

In the code above, we are using the time.LoadLocation() function to load the "America/New_York" time zone. We use the time.ParseInLocation() function to parse a string representation of a date in the format "2021-08-30T08:30:00" into a time.Time object with the "America/New_York" time zone.

Conclusion

In this guide, we covered how to convert a string to a time.Time object in Go. We explored the time.Parse() and time.ParseInLocation() functions, along with common date and time layouts and handling time zones. This knowledge will help you easily parse date and time strings in your Go web applications and APIs.