To format a date to the format "month day, year hour:minute:second am/pm" in Go, you can use the time
package and its Format
function.
Here is an example code snippet:
main.go166 chars13 lines
In this code snippet, we first obtain the current datetime using time.Now()
. We then use the Format
function to format the datetime into the desired format, which uses predefined layout strings.
The layout string we use is "January 2, 2006 3:04:05 PM"
, which is a reference time of January 2, 2006 at 3:04:05 PM, the format we want. We use this reference time to parse and format the current datetime using the same format symbols.
The output of the above code would be:
main.go28 chars2 lines
which is the current date and time in the format "month day, year hour:minute:second am/pm".
gistlibby LogSnag