Time Formatting / Parsing

This Go code demonstrates formatting and parsing time in different layouts using the time package. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"fmt"
	"time"
)

// The main function, where the execution of the program begins.
func main() {
	// Creating an alias for fmt.Println for brevity.
	p := fmt.Println

	// Getting the current time.
	t := time.Now()
	p(t.Format(time.RFC3339))

	// Parsing a time string in RFC3339 format.
	t1, e := time.Parse(
		time.RFC3339,
		"2012-11-01T22:08:41+00:00")
	p(t1)

	// Formatting time in custom layouts.
	p(t.Format("3:04PM"))
	p(t.Format("Mon Jan _2 15:04:05 2006"))
	p(t.Format("2006-01-02T15:04:05.999999-07:00"))

	// Parsing a time string with a custom layout.
	form := "3 04 PM"
	t2, e := time.Parse(form, "8 41 PM")
	p(t2)

	// Formatting time components manually.
	fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
		t.Year(), t.Month(), t.Day(),
		t.Hour(), t.Minute(), t.Second())

	// Parsing a time string in a different layout with an error check.
	ansic := "Mon Jan _2 15:04:05 2006"
	_, e = time.Parse(ansic, "8:41PM")
	p(e)
}

Output

2024-01-24T13:29:21+05:30
2012-11-01 22:08:41 +0000 +0000
1:29PM
Wed Jan 24 13:29:21 2024
2024-01-24T13:29:21.122407+05:30
0000-01-01 20:41:00 +0000 UTC
2024-01-24T13:29:21-00:00
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": cannot parse "8:41PM" as "Mon"

Explanation:

  1. Getting Current Time:

    • time.Now() returns the current time.

  2. Formatting and Parsing RFC3339:

    • Format is used to format the current time in RFC3339 layout.

    • Parse is used to parse a time string in RFC3339 format.

  3. Formatting in Custom Layouts:

    • Format is used to format the current time in various custom layouts.

  4. Parsing in Custom Layout:

    • Parse is used to parse a time string with a custom layout.

  5. Formatting Time Components Manually:

    • Components of the time are formatted manually.

  6. Parsing in Different Layout with Error Check:

    • Parse is used to parse a time string in a different layout with an error check.

This code showcases how to format and parse time in various layouts using the time package in Go. It provides flexibility in handling different time formats based on specific requirements.

Last updated