Working with Dates in Go (Golang): Overview of Libraries and Packages

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Dates in Go (Golang): A Complete Overview of Libraries



Working with dates and times is one of the most common tasks in web development, analytics, and backend development. In Go (Golang), the standard library time provides a powerful but not always intuitive toolkit. To simplify development, there are many third-party libraries. In this article, we will thoroughly examine the main approaches and popular libraries for working with dates in Go.



1. The Standard time Package



The time package is the foundation for working with dates and times in Go. It includes the Time, Duration, Location types and functions for formatting, parsing, and arithmetic.



Main Features:

  • Current time: time.Now()
  • Formatting: the .Format(layout string) method — uses a special reference format Mon Jan 2 15:04:05 MST 2006 (Go was born in 2006).
  • Parsing: the time.Parse(layout, value string) function.
  • Arithmetic: methods .Add(d Duration), .Sub(t Time), .AddDate(y, m, d int).
  • Time zones: time.LoadLocation(name string) and the .In(loc *Location) method.


package main

import ( "fmt" "time")

func main() { // Current time now := time.Now() fmt.Println("Now:", now)

// Formatting fmt.Println("RFC3339 format:", now.Format(time.RFC3339)) fmt.Println("Custom format:", now.Format("02.01.2006 15:04:05"))

// Parsing a string parsed, _ := time.Parse("2006-01-02", "2024-03-15") fmt.Println("Parsed:", parsed)

// Working with time zones loc, _ := time.LoadLocation("Europe/Moscow") moscowTime := now.In(loc) fmt.Println("Time in Moscow:", moscowTime)

// Arithmetic tomorrow := now.AddDate(0, 0, 1) fmt.Println("Tomorrow:", tomorrow.Format("02.01.2006"))}


Peculiarity: Go uses a unique reference format (2006-01-02T15:04:05Z07:00), rather than the familiar YYYY-MM-DD. This requires getting used to, but it eliminates ambiguities.



2. The carbon Library (Analogous to Carbon for PHP)



The carbon library is a port of the popular PHP library Carbon. It provides a convenient fluent interface for working with dates.



Installation:

go get github.com/golang-module/carbon/v2


Usage Examples:

package main

import ( "fmt" "github.com/golang-module/carbon/v2")

func main() { // Current date now := carbon.Now() fmt.Println("Now:", now.ToDateTimeString())

// Creating a date date := carbon.CreateFromDate(2024, 3, 15) fmt.Println("Date:", date.ToDateString())

// Difference between dates start := carbon.Parse("2024-01-01") end := carbon.Parse("2024-03-15") diff := start.DiffInDays(end) fmt.Printf("Days between: %d", diff)

// Human-readable formats fmt.Println("Yesterday:", carbon.Yesterday().ToDateString()) fmt.Println("Tomorrow:", carbon.Tomorrow().ToDateString())

// Checks fmt.Println("Is it a weekend now?", now.IsWeekend()) fmt.Println("Is it morning now?", now.IsMorning())}


Advantages: simple API, support for Chinese characters, localization, methods for working with the beginning/end of the day, week, month.



3. The now Library (Minimalistic Tool)



The now library by the author of GORM is a lightweight tool for calculating the beginning and end of time periods.



Ins

Recommendations