DEV Community

Devtonight
Devtonight

Posted on • Originally published at devtonight.com

How To Format Date And Time With Go Language

Date and time formatting with Golang can be quite confusing due to its weird formatting patterns. But there are some easy methods that instantly give you full control of handling date and time with Go language.

First, install Go language and create a new Go project if you have not yet created one.

Time Formatting

The following code will print the current unformatted time.

t := time.Now()

fmt.Println(t)  // 2022-01-06 00:00:00 +0000 UTC m=+0.000000001
Enter fullscreen mode Exit fullscreen mode

Seconds

The following methods can be used to get the current second.

t := time.Now()

fmt.Println(t.Format("05"))
fmt.Println(t.Second())
Enter fullscreen mode Exit fullscreen mode

Minutes

The following methods will print the current minute.

t := time.Now()

fmt.Println(t.Format("04"))
fmt.Println(t.Minute())
Enter fullscreen mode Exit fullscreen mode

Hours

The following codes will print the current hour in both 12 and 24-hour formats.

t := time.Now()

fmt.Println(t.Format("03"))  // 12 hours
fmt.Println(t.Format("15"))  // 24 hours
fmt.Println(t.Hour())        // 24 hours
Enter fullscreen mode Exit fullscreen mode

Date Formatting

Day

The following code will print the current day in both short and long formats, such as “Sun” and “Sunday”.

t := time.Now()

fmt.Println(t.Format("Mon"))
fmt.Println(t.Format("Monday"))
Enter fullscreen mode Exit fullscreen mode

Date

The following code will print the current date, such as 05, 18, 24.

t := time.Now()

fmt.Println(t.Day())
Enter fullscreen mode Exit fullscreen mode

Month

The following code will print the current month as a number and full month name.

t := time.Now()

fmt.Println(t.Format("01"))
fmt.Println(t.Month())
Enter fullscreen mode Exit fullscreen mode

Year

The following code will print the current year in both short and long formats.

t := time.Now()

fmt.Println(t.Format("06"))    // 08
fmt.Println(t.Format("2006"))  // 2010
fmt.Println(t.Year())          // 2022
Enter fullscreen mode Exit fullscreen mode

Popular Date-Time Formats

We can use combinations of previously used methods to re-create popular date and time formats.

Hour, Minute And Second

The following code will print the current time in the Hour:Minute:Second format.

t := time.Now()

fmt.Println(t.Format("15:04:05"))
Enter fullscreen mode Exit fullscreen mode

Year, Month And Date

The following code will print the current date in Date-Month-Year and Year-Month-Date formats.

t := time.Now()

fmt.Println(t.Format("02-01-2006"))
fmt.Println(t.Format("2006-01-02"))
Enter fullscreen mode Exit fullscreen mode

Time-Zone Adjustment

We can use the LoadLocation() method to offset the timezone.

t := time.Now()
location, err := time.LoadLocation("UTC")

if err != nil {
    return
}

fmt.Println(t.In(location).Format("2006-01-02 15:04:05"))
Enter fullscreen mode Exit fullscreen mode

Visit pkg.go.dev/time for more information about the Go Time package.

Feel free to visit devtonight.com for more related content.

Discussion (0)