Working with Dates in Go (Golang): time, carbon, and now Libraries

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): Library Overview



Working with dates and times is a key task in any programming language. In Go (Golang), the standard library provides a powerful time package, but sometimes developers turn to third-party libraries for convenience. In this article, we will break down the main capabilities of the standard package and look at two popular libraries: carbon and now.



1. Standard time Package



The time package is the foundation for working with dates in Go. It includes the Time and Duration types, as well as functions for parsing, formatting, working with timers, and time zones.



Creating and Formatting a Date



package main

import ( "fmt" "time")

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

// Creating a specific date date := time.Date(2025, time.March, 15, 14, 30, 0, 0, time.UTC) fmt.Println("Specific date:", date)

// Formatting (must use "2006-01-02 15:04:05" as reference) fmt.Println("Format:", date.Format("02.01.2006 15:04:05"))}


Parsing a String into a Date



func main() {    // Parsing with format specification    str := "2025-03-15 14:30:00"    parsed, err := time.Parse("2006-01-02 15:04:05", str)    if err != nil {        panic(err)    }    fmt.Println("Parsed:", parsed)}


Working with Time Zones



func main() {    loc, _ := time.LoadLocation("Europe/Moscow")    moscowTime := time.Now().In(loc)    fmt.Println("Moscow time:", moscowTime)}


Difference Between Dates



func main() {    start := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)    end := time.Date(2025, 12, 31, 23, 59, 59, 0, time.UTC)    duration := end.Sub(start)    fmt.Printf("Difference: %v (hours: %v)", duration, duration.Hours())}


2. The carbon Library



The carbon library (github.com/golang-module/carbon) is a port of the popular PHP Carbon library for Go. 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.ToString())

// Creating a date date := carbon.Parse("2025-03-15 14:30:00") fmt.Println("Parsed:", date.ToDateString())

// Adding days future := carbon.Now().AddDays(10) fmt.Println("In 10 days:", future.ToDateString())

// Difference in days diff := carbon.Now().DiffInDays(carbon.Parse("2025-12-31")) fmt.Printf("Days until New Year: %d days", diff)

// Formatting fmt.Println("Custom format:", carbon.Now().Format("d/m/Y H:i:s"))}


Convenient Methods



  • StartOfDay() / EndOfDay() — start/end of day
  • IsWeekend() / IsWeekday() — day of week check
  • Age() — age from date of birth
  • DiffForHumans() — human-readable difference (e.g., "3 days ago")


3. The now Library



The now library (github.com/jinzhu/now) from the creator of GORM is a lightweight utility for getting the beginning and end of time periods.



Installation



go get github.com/jinzhu/now


Usage Examples



package main

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

func main() { // Beginning of day beginningOfDay := now.BeginningOfDay() fmt.Println("

Recommendations