Learn complete time package in Go

2 min read
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println //just to make things easy
currtime := time.Now()
p("current time: ", currtime)
p("Year: ", currtime.Year())
p("Month: ", currtime.Month())// prints the month in string format
p("Day: ", currtime.Day())
p("Hour: ", currtime.Hour())
p("Minute: ", currtime.Minute())
p("Second: ", currtime.Second())
p("Nanosecond: ", currtime.Nanosecond())
p("Location: ", currtime.Location())
p(currtime.Zone()) // prints zone like "IST"
//let's print which day it is
p("__day: ", currtime.Weekday())
//let's print which week it is
p(currtime.ISOWeek())
//creating a custom time using time.Date function
pasttime := time.Date(2021, 8, 15, 14, 30, 45, 100 , time.Local)
p(pasttime)
//formatting time using Format function
p(pasttime.Format("2006:01:02 03:04:05 pm"))
//using the RFC3339 format
p(pasttime.Format(time.RFC3339Nano))
//let's compare both the time current time and past time
p(pasttime.Before(currtime))// should be true
p(pasttime.Equal(currtime))// should be false
p(pasttime.After(currtime))// should be false
//let's find the difference in time
diff := currtime.Sub(pasttime)
p("difference in time: ", diff)
p("hour difference : ", diff.Hours())
p("Minute difference: ",diff.Minutes())
p("Second difference: ",diff.Seconds())
p("Nanosecond difference: ",diff.Nanoseconds())
p(pasttime.Add(diff))// should get currtime
p(currtime.Add(-diff))// should get pasttime
unixSeconds := currtime.Unix()// this is the elapsed time since Unix epoch in seconds
unixMilli := currtime.UnixMilli()// same ^ but in milliseconds
unixNano := currtime.UnixNano()// same in nanoseconds
p(unixSeconds)
p(unixMilli)
p(unixNano)
//let's convert unix time to *time.Time
p(time.Unix(unixSeconds, 0))
p(time.UnixMilli(unixMilli))
p(time.Unix(0, unixNano))
// UTC is the standard time zone which is used in applications
utcTime:= currtime.UTC()
p("UTC time is: ", utcTime)
// to convert it back to Local time
localTime := utcTime.Local()
p("The local time is: ", localTime)
}
current time: 2025-07-29 20:50:54.367091 +0530 IST m=+0.000511001
Year: 2025
Month: July
Day: 29
Hour: 20
Minute: 50
Second: 54
Nanosecond: 367091000
Location: Local
IST 19800
__day: Tuesday
2025 31
2021-08-15 14:30:45.0000001 +0530 IST
2021:08:15 02:30:45 pm
2021-08-15T14:30:45.0000001+05:30
true
false
false
difference in time: 34662h20m9.3670909s
hour difference : 34662.335935303025
Minute difference: 2.0797401561181818e+06
Second difference: 1.247844093670909e+08
Nanosecond difference: 124784409367090900
2025-07-29 20:50:54.367091 +0530 IST
2021-08-15 14:30:45.0000001 +0530 IST m=-124784409.366579899
1753802454
1753802454367
1753802454367091000
2025-07-29 20:50:54 +0530 IST
2025-07-29 20:50:54.367 +0530 IST
2025-07-29 20:50:54.367091 +0530 IST
UTC time is: 2025-07-29 15:20:54.367091 +0000 UTC
The local time is: 2025-07-29 20:50:54.367091 +0530 IST
0
Subscribe to my newsletter
Read articles from Priyansh Sao directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
