Home >Backend Development >Golang >How Can I Convert Dates to Custom Formats in Go?
Converting Dates to Custom Formats in Go
When handling dates, it's often necessary to convert them into different formats for various purposes. For example, you may encounter a date in the format of "2010-01-23 11:44:20" and need to display it as "Jan 23 '10 at 11:44."
The Go programming language provides the time package, which includes functions for parsing and formatting dates. To convert a date to a custom format, you can utilize the Parse and Format methods.
Solution:
Here's an example code snippet:
import "time" func main() { dtstr1 := "2010-01-23 11:44:20" dt, _ := time.Parse("2006-01-02 15:04:05", dtstr1) dtstr2 := dt.Format("Jan 2 '06 at 15:04") // Display the converted date fmt.Println(dtstr2) }
The above is the detailed content of How Can I Convert Dates to Custom Formats in Go?. For more information, please follow other related articles on the PHP Chinese website!