Time formatting in Go (time to string)

T

Dealing with date and time values ​​is a common practice in the development world. Like many other programming languages, Go has features and ways to deal with date values.

A common task when working with dates is to format the date string in a specific layout. In this tutorial, we will learn how to format time strings in Go using the given methods and features.

go time format

Before we get into time formatting using Go, let's explore the different time formats supported in the language.

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

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

  • Mon – weekday abbreviation
  • January – short month name
  • 2 – day of the month (zero-padded)
  • 15 – Hour (zero padded, 24 hour clock)
  • 04 – minutes (zero-padded)
  • 05 – Second (zero padded)
  • -0700 – time zone offset (for example, -0700 for MST)
  • 2006 – year

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

go format time

Let's explore the different methods we can use to format the time:

Using Time.Format Method

The first and most common way to format time is to use the Format() method of the time.Time() type. This 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”
,
main work,, ,
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 format “2006-01-02 15:04:05”.

Example output:

general format pattern

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

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

custom format pattern

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

  • 02/01/2006 – Date in “DD/MM/YYYY” format
  • January 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 the time format you want which is useful in a complex application.

Using Time.Time for String Functions

We can also use “time.time” for string functions like “string”, “stringlocal” and “stringUTC”. These methods allow us to convert “time.Time” values ​​into string representations. However, these methods use predefined format layouts as shown in the following example:

package main
Import ,

“fmt”

“Time”

main work,, ,
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 eat +0300 M,0.000465043
2023,11,30 08:14,17.705958 eat +0300
2023,11,30 05:14,17.705958 +0000 UTC

In this case, the “String” function provides a default format, while “StringLocal” and “StringUTC” provide 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.

Add comment

By Ranjan