Home >Backend Development >Golang >How to Convert Date Formats in Go?
Converting Date Formats in Go
Converting dates between different formats is a common task in software development. In Go, the time package provides a range of functions for manipulating dates and times.
Converting to a Specific Format
One question arose from a user who wished to convert a date from the format 2010-01-23 11:44:20 to Jan 23 '10 at 11:44. This conversion involves parsing the original date string and then using the Format function to generate the desired format.
Solution
To solve this problem, you can use the following code:
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")
The Parse function takes two parameters: the layout of the input date string and the date string itself. The layout string specifies the format of the input date, and the Parse function returns a Time object representing the date.
The Format function takes two parameters: the layout of the desired output and the Time object. The layout string specifies the format of the output, and the Format function returns the date string in the desired format.
The above is the detailed content of How to Convert Date Formats in Go?. For more information, please follow other related articles on the PHP Chinese website!