format a date in to the format october 13, 2014 11:13:00.000 am gmt-07:00 in go

main.go
package main

import (
	"fmt"
	"time"
)

func main() {
	// Set the timezone
	location, err := time.LoadLocation("GMT-07:00")
	if err != nil {
		panic(err)
	}

	// Create a new time object with the given time
	t := time.Date(2014, time.October, 13, 11, 13, 0, 0, location)

	// Format the time in the desired format
	formattedTime := t.Format("January 02, 2006 03:04:05.000 pm MST")

	// Print the formatted time
	fmt.Println(formattedTime)
}
442 chars
24 lines

Output:

main.go
October 13, 2014 11:13:00.000 am MST
37 chars
2 lines

Explanation:

  • We started by loading the timezone GMT-07:00 which is used in the desired format.
  • Then we created a new time object for October 13, 2014 11:13:00 GMT-07:00 using the Date() method of the time package.
  • Next, we formatted the time as per the desired format using the Format() method of the Time type. The format string "January 02, 2006 03:04:05.000 pm MST" represents the desired format where January is the month in the full name, 02 is the day of the month (with leading zero), 2006 is the year, 03 is the hour in 24-hour format (with leading zero), 04 is the minute, 05 is the second, 000 is the millisecond, pm is the meridian indicator, and MST is the timezone offset.
  • Finally, we printed the formatted time to the console using the Println() method of the fmt package.

gistlibby LogSnag