Time package in Golang

Implementation of Time package in Golang!

timepackage

Time package in Golang

In this blog post we'll see how to use the time package in Golang.

The package time feature allows us to measure and display time.

To use the time package in Golang, we need to import the package.

import "time"

For Seconds, Milliseconds, Nanoseconds, Microseconds, and Minutes, we can use the following functions:

For Current Time and Date:

// get current time in UTC timezone
currentTime := time.Now()

// To get the custom time in UTC timezone, we can use the following function:
t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) // year, month, day, hour, minute, second, nanosecond, location
log.Println("Time: ", t)
log.Println("Time in UTC: ", t.UTC())
log.Println("Time in Local: ", t.Local())
log.Println("Time in Location: ", t.In(time.FixedZone("UTC", 0)))
log.Println("Get the time - weekday: ", t.Weekday())
log.Println("Get the time - year: ", t.Year())
log.Println("Get the time - month: ", t.Month())
log.Println("Get the time - day: ", t.Day())
log.Println("Get the time - hour: ", t.Hour())
log.Println("Get the time - minute: ", t.Minute())
log.Println("Get the time - second: ", t.Second())
log.Println("Get the time - nanosecond: ", t.Nanosecond())
log.Println("Get the time - location: ", t.Location())
output:

2022/06/17 17:09:42 Time:  2009-11-10 23:00:00 +0000 UTC
2022/06/17 17:09:42 Time in UTC:  2009-11-10 23:00:00 +0000 UTC     
2022/06/17 17:09:42 Time in Local:  2009-11-11 04:30:00 +0530 IST   
2022/06/17 17:09:42 Time in Location:  2009-11-10 23:00:00 +0000 UTC
2022/06/17 17:09:42 Get the time - weekday:  Tuesday
2022/06/17 17:09:42 Get the time - year:  2009
2022/06/17 17:09:42 Get the time - month:  November
2022/06/17 17:09:42 Get the time - day:  10
2022/06/17 17:09:42 Get the time - hour:  23
2022/06/17 17:09:42 Get the time - minute:  0
2022/06/17 17:09:42 Get the time - second:  0
2022/06/17 17:09:42 Get the time - nanosecond:  0
2022/06/17 17:09:42 Get the time - location:  UTC

For Seconds:

// get current time in seconds since Unix epoch (January 1, 1970 UTC)
secs := time.Now().Unix()

For Milliseconds:

// get current time in milliseconds since Unix epoch (January 1, 1970 UTC)
milliSecs := time.Now().UnixNano() / 1000000

// or 

// get current time in milliseconds since Unix epoch (January 1, 1970 UTC)
milli := time.Now().UnixMilli()

For Microseconds:

// get current time in microseconds since Unix epoch (January 1, 1970 UTC)
microSecs := time.Now().UnixNano() / 1000

For NanoSeconds:

// get current time in nanoseconds since Unix epoch (January 1, 1970 UTC)
nanoSecs := time.Now().UnixNano()

Get time in avaible formats:

// get current time in available formats
// get current time in RFC 3339 format
isoTime := time.Now().Format(time.RFC3339)
log.Println("ISO Time: ", isoTime)

// get current time in RFC 1123 format
rfcTime := time.Now().Format(time.RFC1123)
log.Println("RFC Time: ", rfcTime)

// print all the time formats in the local timezone
log.Println("Time in Local: ", time.Now().Format(time.Kitchen))
log.Println("Time in Local: ", time.Now().Format(time.Stamp))
log.Println("Time in Local: ", time.Now().Format(time.StampMilli))
log.Println("Time in Local: ", time.Now().Format(time.StampMicro))
log.Println("Time in Local: ", time.Now().Format(time.StampNano))
log.Println("Time in Local: ", time.Now().Format(time.RFC3339))
log.Println("Time in Local: ", time.Now().Format(time.RFC1123))
output:

2022/06/17 17:09:42 ISO Time:  2022-06-17T17:09:42+05:30
2022/06/17 17:09:42 RFC Time:  Fri, 17 Jun 2022 17:09:42 IST
2022/06/17 17:09:42 Time in Local:  5:09PM
2022/06/17 17:09:42 Time in Local:  Jun 17 17:09:42
2022/06/17 17:09:42 Time in Local:  Jun 17 17:09:42.984
2022/06/17 17:09:42 Time in Local:  Jun 17 17:09:42.985474
2022/06/17 17:09:42 Time in Local:  Jun 17 17:09:42.985474600
2022/06/17 17:09:42 Time in Local:  2022-06-17T17:09:42+05:30
2022/06/17 17:09:42 Time in Local:  Fri, 17 Jun 2022 17:09:42 IST

To format a time in a specific format, we can use the following function:

// format a time in string in the given layout
const layout = "Mon Jan 2 15:04:05 2006"
var stringDate string
t, _ := time.Parse(layout, stringDate)
date := t.Format("2006-01-02 15:04:05")
t, _ = time.Parse("2006-01-02 15:04:05", date)

Link to the source code: Gist

I hope this blog post will help you to get familiar with Time Package in Golang.

If you have any questions, please leave them in the comment section.

Yash Chauhan | @codingyash
About the author

Yash is a Software Engineer and a Enthusiastic full stack developer, eager to contribute to team success through hard work, attention to detail, and excellent organizational skills, a clear understanding of frontend, backend, and project management.

Copyright © Yash. Learning by Sharing :)