Understanding Ways To Format DateTime in GO Language

DJ
Developers Journal
Published in
3 min readApr 14, 2023

--

DateTime is a type that contains properties of a date and time at a particular instant. Go language provides several ways to format dates and times using the time package.

The time package provides the necessary functionality for telling, measuring, and displaying the time. The time package also contains the primary methods to format, parse, display, and manipulate date and time. Any variable or field in a struct that stores time as a value will be of type time.Time, representing an instant in time with nanosecond precision. For example, the below-specified code outputs the current date and time with all the information on the standard console.

Current DateTime

package main 

import (
"fmt"
"time"
)

func main() {
// current date and time from the time package
currentDateTime := time.Now()
fmt.Printf("The current Date and Time is : \n%v\n", currentDateTime)
}
The Current Date and Time is : 
2023-04-15 00:35:28.874346692 +0530 IST m=+0.000067377

Standard format specifiers and examples of how to use them

For readability purposes, we tend to format a particular date and time; Go provides a time.Time method, , that can be called on a value of type Time. Format is a method that accepts a string layout and transforms the time receiver into a string that follows the specified pattern.

DATE: January 2, 2006–01/02/2006

This is a popular format in the US that displays the date in the month-day-year format. In Go, you can use the layout string “January 2, 2006” or “01/02/2006” to format dates in this way.

package main 

import (
"fmt"
"time"
)

func main() {
t := time.Now()
fmt.Println(t.Format("April 15, 2023"))
fmt.Println(t.Format("04/15/2023"))
}
April 15, 2023 
04/15/2023

This format is commonly used to display the time in hours:minutes:seconds format. In Go, you can use the layout string “15:04:05” to display the time in this format. To display the time in 12-hour format with AM/PM indicator, use the layout string “03:04 PM”.

package main 

import (
"fmt"
"time"
)

func main() {
t := time.Now()
fmt.Println(t.Format("19:14:27"))
fmt.Println(t.Format("07:14 PM"))
}
19:14:27
07:14 PM

Go provides different formats for date and time; however, if you want, you can also use one of the pre-defined Layout constants that come with Go. The layout is the string format passed as an argument to the Format function.

Here are some pre-defined layouts, but please refer to the Go documentation for the complete list.

ISO 8601 — RFC3339:
ISO 8601 is an international standard for representing dates and times. RFC3339 is a profile of ISO 8601 that is commonly used on the internet.

package main 

import (
"fmt"
"time"
)

func main() {
currentDateTime := time.Now()
fmt.Printf("Kitchen: %v\n", currentDateTime.Format(time.Kitchen))
fmt.Printf("RFC1123: %v\n", currentDateTime.Format(time.RFC1123))
fmt.Printf("RFC3339: %v\n", currentDateTime.Format(time.RFC3339))
}
Kitchen: 1:17AM 
RFC1123: Sat, 15 Apr 2023 01:17:00 IST
RFC3339: 2023-04-15T01:19:14+05:30

You can find more information about the layout elements in the Go documentation for the time package.

These are just a few examples of how to format dates and times in Go. For more information, check out the Go documentation for the time package.

Originally published at https://developersjournal.in on April 14, 2023.

--

--

DJ
Developers Journal

Journal for developers who are looking for all the latest information’s, tutorials, what’s new, how-to, and so much more. https://developersjournal.in