| by Arround The Web | No comments

Time Formatting in Go (Time to String)

Dealing with date and time values is a common operation in the world of development. Like many other programming languages, Go has features and methods of dealing with date values.

One common task when working with dates is formatting the date strings into specific layouts. In this tutorial, we will learn how to format the time strings in Go using the provided methods and features.

Go Time Formats

Before we dive into time formatting using Go, let us explore the various time formats that are supported in the language.

Time formatting is done using a reference time which is represented as “Mon Jan 2 15:04:05 -0700 MST 2006”.

This reference time allows us to specify how a “time.Time” value should be formatted or parsed. The reference time is comprised of various components as follows:

  • Mon – Abbreviated weekday name
  • Jan – Abbreviated month name
  • 2 – Day of the month (zero-padded)
  • 15 – Hour (zero-padded, 24-hour clock)
  • 04 – Minute (zero-padded)
  • 05 – Second (zero-padded)
  • -0700 – Time zone offset (e.g., -0700 for MST)
  • 2006 – Year

Think of the reference time as a template to format and parse the time in the Go language. Therefore, when formatting the time value into a string or parsing a string into the “time.Time” object, we can use these components as the reference.

Go Format Time

Let us explore the various methods that we can use to format time:

Using the Time.Format Method

The first and most common method of formatting the time is using the Format() method from the time.Time() type. The method allows us to format the time value into a string using a specified format.

An example usage is as follows:

package main
import (
    "fmt"
    "time"
)
func main() {
    now := time.Now()
    formatted := now.Format("2006-01-02 15:04:05")
    fmt.Println(formatted)
}

 

In this example, we use the Format() method to format the current time as a string using the “2006-01-02 15:04:05” format.

Example output:

2023-11-30 08:08:34

 

Common Format Patterns

Go provides us with a set of common format patterns that we can use with the “Format” method to achieve specific time representations.

  • 2006-01-02 15:04:05 – Full date and time representation
  • 2006-01-02 – Date in the “YYYY-MM-DD” format
  • 15:04:05 – Time in the “HH:MM:SS” format
  • Mon, 02 Jan 2006 15:04:05 MST – A common RFC1123 format for HTTP headers

Custom Format Patterns

We can create custom format patterns by combining the reference time components and additional characters. For example:

  • 02/01/2006 – Date in the “DD/MM/YYYY” format
  • Jan 2, 2006 3:04:05 PM – Date and time in a more readable format
  • Monday, January 2, 2006 – Long-form date with day and month names

These can allow you to customize your desired time format which is useful in a complex application.

Using Time.Time to String Functions

We can also use the “time.Time” to string functions such as “String”, “StringLocal”, and “StringUTC”. These methods allow us to convert the “time.Time” values to string representations. However, these methods use the predefined format layouts as shown in the following example:

package main
import (
    "fmt"
    "time"

func main() {
    now := time.Now()
    str := now.String()
    strLocal := now.Local().String()
    strUTC := now.UTC().String()

    fmt.Println(str)
    fmt.Println(strLocal)
    fmt.Println(strUTC)
}

 

Output:

2023-11-30 08:14:17.705958 +0300 EAT m=+0.000465043
2023-11-30 08:14:17.705958 +0300 EAT
2023-11-30 05:14:17.705958 +0000 UTC

 

In this case, the “String” function provides a default format, while “StringLocal” and “StringUTC” provide the local and UTC time representations, respectively.

Conclusion

In this tutorial, we covered one of the common operations when dealing with date and time values: time formatting using the Go language.

Share Button

Source: linuxhint.com

Leave a Reply