Go allows you to format and parse time out of the box provided by the time package. You can specify your target format using pattern-based layouts using the Format() method. Unlike typical date and time formatting options such as YYYY-MM-DD, go uses a specific layout parameter.

Using this short guide, you will understand how to format time in Golang using various formats and patterns.

The Basics

We use the Format() method and pass a Time object as the parameter to format time in go. The function syntax is as shown:

func (t Time) Format(layout string) string

The function will return the time value formatted to the specified layout. You can define custom formats or use pre-defined ones. These include:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/Goolang-time-format-1.png" data-lazy- height="306" src="data:image/svg xml,” width=”650″>

The layouts use the reference time on Mon Jan 2 15:04:05 MST 2006 to indicate the pattern under which to format the time object.

Example 1 – Custom Layouts

Let us look at a few examples of formatting time using various custom layouts.

func yyyy_mm_dd() {


    current_time := time.Now()


    fmt.Println(current_time.Format(“2006-01-02”))

}

In the example above, we grab the current time using the time.Now() method. Next, we format the current time to YYYY-MM-DD using the reference date.

We can now execute the code above as shown:

$ go run time_format.go

2022-01-17

If we want to include the date and time in the 24-hr system, we can set the format as shown in the snippet below:

current_time := time.Now()


fmt.Println(current_time.Format(“2006-01-02 15:04:05”))

The above code should return the output as:

To display the time in 12hr clock, we can do:

current_time := time.Now()


fmt.Println(current_time.Format(“2006-01-02 3:4:5 pm”))

The code returns the output as:

Example 2 – Pre-Defined Layouts

You can also format according to a pre-defined layout by passing the layout as a string to the Format() Method.

Consider the following examples:

func format_pre_defined() {


    current_time := time.Now()


    fmt.Println(“ANSIC -> “, current_time.Format(time.ANSIC))


    fmt.Println(“UnixDate -> “, current_time.Format(time.UnixDate))


    fmt.Println(“Kitchen -> “, current_time.Format(time.Kitchen))


    fmt.Println(“RFC3339 -> “, current_time.Format(time.RFC3339))


    fmt.Println(“RubyDate -> “, current_time.Format(time.RubyDate))


    fmt.Println(“RFC822 -> “, current_time.Format(time.RFC822))


    fmt.Println(“RFC1123Z -> “, current_time.Format(time.RFC1123Z))

}

Once we run the above code, it should return the current time formatted into various pre-defined formats. An example output is as shown:

ANSIC –>  Mon Jan 17 14:56:03 2022


UnixDate –>  Mon Jan 17 14:56:03 EAT 2022


Kitchen –>  2:56PM


RFC3339 –>  2022-01-17T14:56:03 03:00


RubyDate –>  Mon Jan 17 14:56:03 0300 2022


RFC822 –>  17 Jan 22 14:56 EAT


RFC1123Z –>  Mon, 17 Jan 2022 14:56:03 0300

Closing

This guide covers the basics of time formatting in the go programming language using the Format() method from the time package.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/john-150×150.png621352b67e0ba.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list