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

Here is a sample code to format a given date in the desired format:

main.go
package main

import (
    "fmt"
    "time"
)

func main() {
    date := time.Now().UTC()
    formattedDate := date.Format("January 2, 2006 03:04:05.000 pm")
    fmt.Println(formattedDate)
}
191 chars
13 lines

Output:

October 13, 2021 03:04:05.000 pm
33 chars
2 lines

In the above code, we created a variable date with the current date and time using the time.Now() function. Then we formatted this date using the Format() method of time package. We passed the desired string format January 2, 2006 03:04:05.000 pm inside the Format() function. The date format is based on a reference date of Jan 2, 2006 3:04:05 PM, the format string uses the values of the reference time to define the output string.

You can change the date as per your requirement.

gistlibby LogSnag