format a date in to the format october 13, 2014 11:13:00 am in go

You can format a date in the desired format using the time package in Go. Here is an example code snippet to format a date in the format "October 13, 2014 11:13:00 am":

main.go
package main

import (
	"fmt"
	"time"
)

func main() {
	dateString := "2014-10-13 11:13:00"
	layout := "2006-01-02 15:04:05"
	t, err := time.Parse(layout, dateString)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(t.Format("January 2, 2006 03:04:05 pm"))
}
262 chars
17 lines

Output:

main.go
October 13, 2014 11:13:00 am
29 chars
2 lines

In the code above, we first parse the original date string into a time.Time object using the time.Parse method. We specify the layout to match the format of the input date string.

Once we have the time.Time object, we can format it into our desired output format using the Format method. We specify the layout of the desired output format as an argument to the Format method.

gistlibby LogSnag