Working with Dates in Go (Golang): Overview of Libraries for Parsing, Formatting, and Manipulation

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): Libraries and Tools



The Go language (Golang) offers a powerful standard library time, which covers most tasks related to working with dates and time. However, for complex operations such as parsing non-standard formats, working with relative dates, or calculations with business calendars, the community has created a number of useful libraries. In this article, we will look at both Go's built-in capabilities and the best third-party packages for working with dates.



1. Built-in time Library



The standard time library is the foundation for any date operations in Go. It provides:

  • Time, Duration, Location types
  • Functions for getting the current time, creating dates, parsing, and formatting
  • Support for time zones and monotonic clocks


Getting the Current Time

package main

import ( "fmt" "time")

func main() { now := time.Now() fmt.Println("Current time:", now) fmt.Println("Year:", now.Year()) fmt.Println("Month:", now.Month()) fmt.Println("Day:", now.Day())}


Formatting Dates

Go uses a special reference format: Mon Jan 2 15:04:05 MST 2006 (1,2,3,4,5,6 — digits in ascending order).

now := time.Now()fmt.Println(now.Format("2006-01-02 15:04:05"))   // 2025-03-15 14:30:00fmt.Println(now.Format("02 Jan 2006"))           // 15 Mar 2025fmt.Println(now.Format(time.RFC3339))            // 2025-03-15T14:30:00Z


Parsing Strings

dateStr := "2025-03-15 14:30:00"t, err := time.Parse("2006-01-02 15:04:05", dateStr)if err != nil {    panic(err)}fmt.Println(t) // 2025-03-15 14:30:00 +0000 UTC


Working with Time Zones

loc, _ := time.LoadLocation("Europe/Moscow")moscowTime := time.Now().In(loc)fmt.Println("Moscow time:", moscowTime.Format("15:04:05"))


2. carbon Library



The package github.com/golang-module/carbon is a Go analog of the popular PHP library Carbon. It provides a convenient API for working with dates, including relative dates, human-readable formats, and chained calls.



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.ToDateString()) // 2025-03-15

// Relative dates fmt.Println(now.AddDays(7).ToDateString()) // 2025-03-22 fmt.Println(now.SubMonths(1).ToDateString()) // 2025-02-15 fmt.Println(now.StartOfMonth().ToDateString()) // 2025-03-01 fmt.Println(now.EndOfMonth().ToDateString()) // 2025-03-31

// Human-readable formats fmt.Println(now.DiffForHumans()) // just now

// Parsing with support for multiple formats t := carbon.Parse("2025-03-15 14:30:00") fmt.Println(t.ToDateTimeString()) // 2025-03-15 14:30:00

// Working with time zones cst := carbon.Now(carbon.NewYork) fmt.Println(cst.ToDateTimeString()) // time in New York}


3. now Library



The package github.com/jinzhu/now (by the author of GORM) provides convenient functions for getting the beginning and end of time periods.



Installation

go get github.com/jinzhu/now


Examples

package main

import ( "fmt" "github.com/jinzhu/now" "time")

func main() { t := time.Now() fmt.Println(now.BeginningOfMonth(t)) // 2025-03-01 00:00:00 fmt.Println(now.EndOfMonth(t)) // 2025-03-31 23:59:59 fmt.Println(now.BeginningOfYear(t))

Recommendations