HomeGOUnderstanding Ways To Format DateTime in GO Language

Understanding Ways To Format DateTime in GO Language

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 : %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, Format, 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"))
}

Output:

April 15, 2023
04/15/2023

TIME: 15:04:05 – 03:04 PM

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"))
}

Output:

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.

ConstantFormat Layout
ANSIC“Mon Jan _2 15:04:05 2006”
RFC822“02 Jan 06 15:04 MST”
RFC1123“Mon, 02 Jan 2006 15:04:05 MST”
RFC3339“2006-01-02T15:04:05Z07:00”
Kitchen“3:04PM”

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))
}

Output:

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.

RELATED ARTICLES

Most Popular