Working with Date and Time in Golang
Managing dates and times is a common task in many applications, whether it’s logging events, scheduling tasks, or measuring time intervals. Go, as a modern programming language, offers a robust set of built-in tools for handling date and time efficiently. The time
package in Go provides various utilities to work with time zones, parse and format dates, measure time durations, and perform operations like adding or subtracting time.
In this article, we will explore the basics of working with date and time in Go, from retrieving the current time to formatting and manipulating dates. Whether you’re building a simple application or handling complex time-based operations, Go’s time
package has you covered. Let’s dive into how to manage time in Go and avoid common pitfalls.
Getting current time
You can get the current time using the package time
like this:
package main
import "time"
func main() {
actualTime := time.Now()
fmt.Println(actualTime)
}
It’s that simple, it will return the data to you, but not in the expected format you’re looking for:
2024-15-11 23:00:00 +0000 UTC m=+0.000000001
In this case, Go time
package give to us tools to handle with build in format or custom format, below there are all built in formats to work with data:
Formating Data
- ANSIC Mon Jan 02 15:04:05 2006
- UnixDate Mon Jan 02 15:04:05 MST 2006
- RubyDate Mon Jan 02 15:04:05 -0700 2006
- RFC822 02 Jan 06 15:04 MST
- RFC822Z 02 Jan 06 15:04 -0700
- RFC850 Monday, 02-Jan-06 15:04:05 MST
- RFC1123 Mon, 02 Jan 2006 15:04:05 MST
- RFC1123Z Mon, 02 Jan 2006 15:04:05 -0700
- RFC3339 2006-01-02T15:04:05Z07:00
- RFC3339Nano 2006-01-02T15:04:05.999999999Z07:00
- Kitchen 3:04PM
So let’s day we wanna use the RFC3339
with the actual data, how we achieve this?
package main
import "time"
func main() {
actualTime := time.Now()
formatTime := actualTime.Format(time.RFC3339)
fmt.Println(formatTime)
}
Like this we have the data wit the format that we passed in the function:
2024-15-11T23:00:00Z
Custom Data Format
So let’s say you wanna a specific data format, but none of the built in formats with Go suits you use case, for this, we can build our own format, following some specifics rules:
Layout options
Type | Options |
---|---|
Year | 06 2006 |
Month | 01 1 Jan January |
Day | 02 2 _2 (width two, right justified) |
Weekday | Mon Monday |
Hours | 03 3 15 |
Minutes | 04 4 |
Seconds | 05 5 |
ms μs ns | .000 .000000 .000000000 |
ms μs ns | .999 .999999 .999999999 (trailing zeros removed) |
am/pm | PM pm |
Timezone | MST |
Offset | -0700 -07 -07:00 Z0700 Z07:00 |
Usage
package main
import (
"fmt"
"time"
)
func main() {
timeFormat := "02-Jan-2006 03:00:00"
actualTime := time.Now()
formatedTime := actualTime.Format(timeFormat)
fmt.Println(formatedTime)
}
The output it is like this:
15-Sep-2024 11:00:00
Using timezone
You can also provide for time
function the timezone that you wish to work with, in this example I am using São Paulo timezone:
package main
import (
"fmt"
"time"
)
func main() {
loc, err := time.LoadLocation("America/Sao_Paulo")
if err != nil {
fmt.Println(err)
return
}
//Loading the time with timezone
currentTime := time.Now().In(loc)
//Formating date and time
formattedTime := currentTime.Format("02-01-2006 15:04:05 MST")
fmt.Println("Actual Time in São Paulo:", formattedTime)
}
Adding or decreasing date and time
In Go time
package you can simple add the necessary time for your datetime, let’s say you have to take an actual situation that happens now and after one hour validate this task or information
actualTime := time.Now()
timeToRun := actualTime.Add(1 * time.Hour)
fmt.Printf("Original Time: %v \\n", actualTime)
fmt.Printf("Time added: %v \\n", timeToRun)
The same can it be with days.
actualTime := time.Now()
// Removing 5 days
pastTime := actualTime.Add(-5 * 24 * time.Hour)
fmt.Printf("Original Time: %v \n", actualTime)
fmt.Printf("Time after subtracting 5 days: %v \n", pastTime)
Those are small examples that you can face when dealing with data in your daily projects using Golang, but time
package has much more to offer to handle the problems with data.