Golang Time Format Examples [With Explanation]

Out of the box, Go (Golang) supports time formatting. In this tutorial, we will walk through some examples of working with time formatting in Golang. When formatting a date time, you have to specify pattern-based layouts and use the Format() function in the time package:

func (t Time) Format(layout string) string: Format returns a textual representation of the time value formatted according to the layout defined by the argument. See the documentation for the constant called Layout to see how to represent the layout format.

Advertisement

 

Supported Golang Time Layouts

You can refer golang time format page to get the list of supported cost values to manipulate the time output. Here is a table depicting the same information:

Unit Golang Layout Examples Note
Year 06 21, 81, 01
Year 2006 2021, 1981, 0001
Month January January, February, December
Month Jan Jan, Feb, Dec
Month 1 1, 2, 12
Month 01 01, 02, 12
Day Monday Monday, Wednesday, Sunday
Day Mon Mon, Wed, Sun
Day 2 1, 2, 11, 31
Day 02 01, 02, 11, 31 zero padded day of the month
Day _2 ⎵1, ⎵2, 11, 31 space padded day of the month
Day 002 001, 002, 011, 031, 145, 365, 366 zero padded day of the year
Day __2 ⎵⎵1, ⎵⎵2, ⎵11, ⎵31, 365, 366 space padded day of the year
Part of day PM AM, PM
Part of day pm am, pm
Hour 24h 15 00, 01, 12, 23
Hour 12h 3 1, 2, 12
Hour 12h 03 01, 02, 12
Minute 4 0, 4 ,10, 35
Minute 04 00, 04 ,10, 35
Second 5 0, 5, 25
Second 05 00, 05, 25
10-1 to 10-9 s .0 .000000000 .1, .199000000 Trailing zeros included
10-1 to 10-9 s .9 .999999999 .1, .199 Trailing zeros omitted
Time zone MST UTC, MST, CET
Time zone Z07 Z, +08, -05 Z is for UTC
Time zone Z0700 Z, +0800, -0500 Z is for UTC
Time zone Z070000 Z, +080000, -050000 Z is for UTC
Time zone Z07:00 Z, +08:00, -05:00 Z is for UTC
Time zone Z07:00:00 Z, +08:00:00, -05:00:00 Z is for UTC
Time zone -07 +00, +08, -05
Time zone -0700 +0000, +0800, -0500
Time zone -070000 +000000, +080000, -050000
Time zone -07:00 +00:00, +08:00, -05:00
Time zone -07:00:00 +00:00:00, +08:00:00, -05:00:00

 

Supported Golang Time Formats

You can refer golang time format page to get the list of supported cost values to manipulate the time output. Here is a table depicting the same information:

Format Example
ANSIC Tue Oct 4 18:54:28 2022
UnixDate Tue Oct 4 18:54:28 +07 2022
RubyDate Tue Oct 04 18:54:28 +0700 2022
RFC822 04 Oct 22 18:54 +07
RFC822Z 04 Oct 22 18:56 +0700
RFC850 Tuesday, 04-Oct-22 18:54:28 +07
RFC1123 Tue, 04 Oct 2022 18:54:28 +07
RFC1123Z Tue, 04 Oct 2022 18:54:28 +0700
RFC3339 2022-10-04T18:54:28+07:00
RFC3339Nano 2022-10-04T18:54:28.0977814+07:00

 

Format time output with a pre-defined format

Based on the above formats, we can easily format a time value using any of those constants. Let's look at an example.

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	fmt.Println(now.Format(time.ANSIC))
	fmt.Println(now.Format(time.UnixDate))
	fmt.Println(now.Format(time.RubyDate))
	fmt.Println(now.Format(time.RFC822))
	fmt.Println(now.Format(time.RFC822Z))
	fmt.Println(now.Format(time.RFC850))
	fmt.Println(now.Format(time.RFC1123))
	fmt.Println(now.Format(time.RFC1123Z))
	fmt.Println(now.Format(time.RFC3339))
	fmt.Println(now.Format(time.RFC3339Nano))
}

Output:

Tue Oct  4 18:56:48 2022
Tue Oct  4 18:56:48 +07 2022
Tue Oct 04 18:56:48 +0700 2022
04 Oct 22 18:56 +07
04 Oct 22 18:56 +0700
Tuesday, 04-Oct-22 18:56:48 +07
Tue, 04 Oct 2022 18:56:48 +07
Tue, 04 Oct 2022 18:56:48 +0700
2022-10-04T18:56:48+07:00
2022-10-04T18:56:48.74371+07:00

 

Format a time with a customized layout (YYYYMMDDHHMMSS)

In the below example, we will use Format() function to change the date time's format. We have to define the layout first, then pass it as parameter to Format() function:

package main

import (
	"fmt"
	"time"
)

func displayDate(format string) {
	fmt.Println(time.Now().Format(format))
}

func main() {
	// The output time will vary for you
	// but you can check the time format in which we get the output
	displayDate("20060102150405")                    // YYYYMMDDHHMMSS Format
	displayDate("2006/01/02 15/04/05")               // YYYY/MM/DD HH/MM/SS Format
	displayDate("02/01/2006 15/04/05")               // DD/MM/YYYY HH/MM/SS Format
	displayDate("02/01/06 15/04/05")                 // DD/MM/YY HH/MM/SS Format
	displayDate("2006-01-02 15:04:05")               // YYYY-MM-DD HH:MM:SS Format
	displayDate("15:04:05, 2006-Jan-02 Mon")         // HH:MM:SS, YYYY-Mon-DD DayShort Format
	displayDate("15:04:05, 2006-Jan-02 Monday")      // HH:MM:SS, YYYY-Mon-DD DayLong Format
	displayDate("15:04:05, 2006-January-02 MST Mon") // HH:MM:SS, YYYY-Mon-DD TZ DayShort Format
	displayDate("3:4:05, 2006-1-02 MST Mon")         // H:M:SS, YYYY-M-DD TZ DayShort Format
	displayDate("3:4:05 pm, 2006-1-02 MST Mon")      // H:M:SS Part_of_day(small), YYYY-M-DD TZ DayShort Format
	displayDate("3:4:05 PM, 2006-1-02 MST Mon")      // H:M:SS Part_of_day(small), YYYY-M-DD TZ DayShort Format
	displayDate("03:04:05.000")                      // HH:MM:SS.MilliSeconds
	displayDate("03:04:05.000000")                   // HH:MM:SS.MicroSeconds
	displayDate("03:04:05.000000000")                // HH:MM:SS.NanoSeconds
}

Output:

Advertisement
# go run main.go 
20221007002045
2022/10/07 00/20/45
07/10/2022 00/20/45
07/10/22 00/20/45
2022-10-07 00:20:45
00:20:45, 2022-Oct-07 Fri
00:20:45, 2022-Oct-07 Friday
00:20:45, 2022-October-07 IST Fri
12:20:45, 2022-10-07 IST Fri
12:20:45 am, 2022-10-07 IST Fri
12:20:45 AM, 2022-10-07 IST Fri
12:20:45.618
12:20:45.618877
12:20:45.618882881

Time is measured in nanosecond precision, but the computer may not be able to measure time passing at this resolution, so steps in time may happen in multiple nanoseconds. The time package has these durations built-in:

  • Nanosecond Duration = 1
  • Microsecond = 1000 * Nanosecond
  • Millisecond = 1000 * Microsecond
  • Second = 1000 * Millisecond
  • Minute = 60 * Second
  • Hour = 60 * Minute

 

Golang time parse format from strings

The time package provides support for creating Time values from strings

  • Parse(layout, str): This function parses a string using the specified layout to create a Time value. An error is returned to indicate problems parsing the string.
  • <strong>ParseInLocation(layout, str, location)</strong>: This function parses a string, using the specified layout and using the Location if no time zone is included in the string. An error is returned to indicate problems parsing the string.

 

Using Parse() function

The Parse function assumes that dates and times expressed without a time zone are defined in Coordinated Universal Time (UTC). The ParseInLocation method can be used to specify a location that is used when no time zone is specified

func Parse(layout, value string) (Time, error): Parse parses a formatted string and returns the time value it represents. The second argument must be parseable using the format string (layout) provided as the first argument.

We can use a pre-defined layout, as shown in Example 1, or a customized one, as shown in Example 2. Here's an example of a string being parsed to a specific data format using Parse() function:

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("Time parsing")

	dateString := "2014-11-09T12:40:45.15Z"
	dateString2 := "2022-10-12T10:15:16.371+07:00"

	time1, err := time.Parse(time.RFC3339, dateString)
	if err != nil {
		fmt.Println("Error while parsing date :", err)
	}
	fmt.Println(time1)

	time2, err := time.Parse(time.RFC3339, dateString2)
	if err != nil {
		fmt.Println("Error while parsing date :", err)
	}
	fmt.Println(time2)

	dateString3 := "2007-10-09 22:50:02.023"
	time3, err := time.Parse("2006-01-02 15:04:05.000", dateString3)
	if err != nil {
		fmt.Println("Error while parsing date :", err)
	}
	fmt.Println(time3)
}

Output:

Time parsing
2014-11-09 12:40:45.15 +0000 UTC
2022-10-12 10:15:16.371 +0700 +07
2007-10-09 22:50:02.023 +0000 UTC

 

Using ParseInLocation() function

func ParseInLocation(layout, value string, loc *Location) (Time, error): ParseInLocation is like Parse but differs in two important ways. First, in the absence of time zone information, Parse interprets a time as UTC; ParseInLocation interprets the time as in the given location. Second, when given a zone offset or abbreviation, Parse tries to match it against the Local location; ParseInLocation uses the given location.

Here's an example convert a string to a specific data format, consider the location and time zone:

package main

import (
	"fmt"
	"time"
)

func PrintTime(label string, t *time.Time) {
	fmt.Println(label, t.Format(time.RFC822Z))
}
func main() {
	// Choose your layout MM YYY DD HH:SS
	layout := "02 Jan 06 15:04"
	// Format this date with Location Timezone
	date := "09 Jun 22 19:30"
	// Load the locations to Parse
	india, inerr := time.LoadLocation("Asia/Kolkata")
	newyork, nycerr := time.LoadLocation("America/New_York")
	if inerr == nil && nycerr == nil {
		nolocation, _ := time.Parse(layout, date)
		indiaTime, _ := time.ParseInLocation(layout, date, india)
		newyorkTime, _ := time.ParseInLocation(layout, date, newyork)
		PrintTime("No location:", &nolocation)
		PrintTime("India:", &indiaTime)
		PrintTime("New York:", &newyorkTime)
	} else {
		fmt.Println(inerr.Error(), nycerr.Error())
	}

}

Output:

$ go run main.go 
No location: 09 Jun 22 19:30 +0000
India: 09 Jun 22 19:30 +0530
New York: 09 Jun 22 19:30 -0400

 

Using time.ParseDuration() function

The time.ParseDuration function parses strings to create Duration values. This function returns a Duration and an error, indicating if there were problems parsing the specified string.

Following table describes the Duration String Unit Indicators

Unit Description
h This unit denotes hours.
m This unit denotes minutes.
s This unit denotes seconds.
ms This unit denotes milliseconds.
us or μs These units denotes microseconds.
ns This unit denotes nanoseconds.

No spaces are allowed between values, which can be specified as integer or floating-point amounts.

Advertisement
package main

import (
	"fmt"
	"time"
)

func Printfln(template string, values ...interface{}) {
	fmt.Printf(template+"\n", values...)
}

func main() {
	d, err := time.ParseDuration("1h30m")
	if err == nil {
		Printfln("Hours: %v", d.Hours())
		Printfln("Mins: %v", d.Minutes())
		Printfln("Seconds: %v", d.Seconds())
		Printfln("Millseconds: %v", d.Milliseconds())
	} else {
		fmt.Println(err.Error())
	}
}

The string specifies 1 hour and 30 minutes.

$ go run main.go 
Hours: 1.5
Mins: 90
Seconds: 5400
Millseconds: 5400000 

 

Summary

In this article, I demonstrated how to perform golang time format using a predefine layout such as YYYYDDMM HH:MM:SS or print the time in nanoseconds, milliseconds, microseconds etc. Convert a string to a specific date format with time.Parse() and time.Format(). We can use either predefined or custom layouts.

 

References

https://www.timeanddate.com/time/map/
https://pkg.go.dev/time#ParseInLocation

 

Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X