How to Format Current Date & Time in Golang

Go programming language has an in-built package called time that provides various functionalities to process date and time.

This guide would help you quickly learn to get the current date and time and convert it into different formats.

Get current date and time

As said before, we will use Go’s time package to get the current time. The time package exports Now() function which returns the local time.

package main

import (
	"fmt"
	"time"
)

func main() {
	dt := time.Now()
	fmt.Println("Current Date & Time: ", dt.String())
}

Copy and paste the code to a main.go file. (Please make sure that the file is placed in GOPATH)

go run main.go

Then run the code with the above command to get this output:

Current Date & Time: 2019-04-11 13:34:22.005450116 +0530 IST m=+0.000249155

Format current date & time

Golang’s time package provides the following set of predefined layouts which can be used to quickly convert time to a standard format.

  • ANSIC
  • UnixDate
  • RubyDate
  • RFC822
  • RFC822Z
  • RFC850
  • RFC1123
  • RFC1123Z
  • RFC3339
  • RFC3339Nano
  • Kitchen
  • Stamp
  • StampMilli
  • StampMicro
  • StampNano

Here’s a quick code that converts current time to the above predefined formats.

package main

import (
	"fmt"
	"time"
)

func main() {
	dt := time.Now()

	fmt.Println("ANSIC:", dt.Format(time.ANSIC))
	fmt.Println("UnixDate:", dt.Format(time.UnixDate))
	fmt.Println("RubyDate:", dt.Format(time.RubyDate))
	fmt.Println("RFC822:", dt.Format(time.RFC822))
	fmt.Println("RFC822Z:", dt.Format(time.RFC822Z))
	fmt.Println("RFC850:", dt.Format(time.RFC850))
	fmt.Println("RFC1123:", dt.Format(time.RFC1123))
	fmt.Println("RFC1123Z:", dt.Format(time.RFC1123Z))
	fmt.Println("RFC3339:", dt.Format(time.RFC3339))
	fmt.Println("RFC3339Nano:", dt.Format(time.RFC3339Nano))
	fmt.Println("Kitchen:", dt.Format(time.Kitchen))
	fmt.Println("Stamp:", dt.Format(time.Stamp))
	fmt.Println("StampMilli:", dt.Format(time.StampMilli))
	fmt.Println("StampMicro:", dt.Format(time.StampMicro))
	fmt.Println("StampNano:", dt.Format(time.StampNano))
}

If you run the above program, it would print something like this,

ANSIC: Thu Apr 11 15:37:31 2019
UnixDate: Thu Apr 11 15:37:31 IST 2019
RubyDate: Thu Apr 11 15:37:31 +0530 2019
RFC822: 11 Apr 19 15:37 IST
RFC822Z: 11 Apr 19 15:37 +0530
RFC850: Thursday, 11-Apr-19 15:37:31 IST
RFC1123: Thu, 11 Apr 2019 15:37:31 IST
RFC1123Z: Thu, 11 Apr 2019 15:37:31 +0530
RFC3339: 2019-04-11T15:37:31+05:30
RFC3339Nano: 2019-04-11T15:37:31.847349524+05:30
Kitchen: 3:37PM
Stamp: Apr 11 15:37:31
StampMilli: Apr 11 15:37:31.847
StampMicro: Apr 11 15:37:31.847349
StampNano: Apr 11 15:37:31.847349524

Most of the time, you’d get the layout of your choice from the above list. But what would you do if you need a very different format?

Creating a new layout in Golan is dead simple.

You just have to remember a reference time which is to be used in the layout.

The reference time is Mon Jan 2 15:04:05 MST 2006 which is Unix time 1136239445

So, here’s a sample code that would print the current date in this format: Monday, 2-January-2006

package main

import (
	"fmt"
	"time"
)

func main() {
	dt := time.Now()

	fmt.Println("Custom Format:", dt.Format("Monday, 2-January-2006"))
}

Here’s the output:

Custom Format: Thursday, 11-April-2019

Final Words

So, it’s not that difficult to get and convert date to whatever format you want in Golang. Please explore time package for more hidden secrets and comment below if you have any queries.

Leave a Comment