ZetCode

Go current time

last modified July 9, 2020

Go current time tutorial shows how to display current date and time in Golang. The time package provides functionality for measuring and displaying time.

The Now function returns the current local time.

The Format function returns a textual representation of the time value formatted according to layout. The layout is either a predefined constant value or a specific format of the reference datetime: Mon Jan 2 15:04:05 -0700 MST 2006.

Go current time example

The following example print the current date and time.

current_time.go
package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    fmt.Println("The current datetime is:", now)
}

The example prints the current time with Now.

$ go run current_time.go 
The current datetime is: 2020-05-26 18:56:03.268250331 +0200 CEST m=+0.000060798

This is a sample output.

Go current time parts

In the following example, we print the parts of the current time.

current_time_parts.go
package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    fmt.Println("Year:", now.Year())
    fmt.Println("Month:", now.Month())
    fmt.Println("Day:", now.Day())
    fmt.Println("Hour:", now.Hour())
    fmt.Println("Minute:", now.Minute())
    fmt.Println("Second:", now.Second())
    fmt.Println("Nanosecond:", now.Nanosecond())
}

In the example, we print the year, month, day, hour, minute, second, and nanosecond with the corresponding functions separately.

$ go run current_time_parts.go 
Year: 2020
Month: May
Day: 26
Hour: 19
Minute: 1
Second: 12
Nanosecond: 985372280

This is a sample output.

Go format current time

Go does not use the typical yyyy-mm-dd format specifier; it uses the following reference datetime format:

Mon Jan 2 15:04:05 -0700 MST 2006

We format the time how we structure this specific reference datetime.

current_time_format.go
package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    fmt.Println("Time: ", now.Format("15:04:05"))
    fmt.Println("Date:", now.Format("Jan 2, 2006"))
    fmt.Println("Timestamp:", now.Format(time.Stamp))
    fmt.Println("ANSIC:", now.Format(time.ANSIC))
    fmt.Println("UnixDate:", now.Format(time.UnixDate))
    fmt.Println("Kitchen:", now.Format(time.Kitchen))
}

The example displays the current time in custom and predefined formats.

fmt.Println("Date:", now.Format("Jan 2, 2006"))

This is an example of a custom datetime format.

fmt.Println("ANSIC:", now.Format(time.ANSIC))

This is an example of a predefined format.

$ go run current_time_format.go 
Time:  19:07:53
Date: May 26, 2020
Timestamp: May 26 19:07:53
ANSIC: Tue May 26 19:07:53 2020
UnixDate: Tue May 26 19:07:53 CEST 2020
Kitchen: 7:07PM

This is a sample output.

In this tutorial, we have shown how to display current datetime in Golang.

List all Go tutorials.