Welcome To Golang By Example

Date in Go (Golang)

Date in Go is represented using time.Time struct only. There is no separate Date struct in Go. time.Date function can be used to construct a date. This function returns the time which is yyyy-mm-dd hh:mm:ss + nsec nanoseconds with the appropriate time zone corresponding to the given location. The signature of the function is:

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time

As can be seen from the signature the arguments to the function are

Some points worth noting about time.Date function

Let’s see a working example:

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2021, time.Month(2), 21, 1, 10, 30, 0, time.UTC)
    fmt.Println(t)
}

Output:

2021-02-21 01:10:30 +0000 UTC