Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

Date in Go (Golang)

Posted on February 1, 2020February 1, 2020 by admin

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

  • Year
  • Month
  • Day
  • Hour
  • Min
  • Sec
  • Millisecond
  • Location

Some points worth noting about time.Date function

  • If the location passed is nil, the Date function will panic
  • The month, day, hour, min, sec, nsec values are normalized. So if we pass a month 14 it will be converted to 2

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
Follow @golangbyexample

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme