How to format time in Go

September 25, 2021 1 min read

Formatting time in go can be quite tricky, luckily there is a handy library called go-humanize that makes formatting time much easier.

Add the library to your project with the command below.

go install github.com/dustin/go-humanize

Use the library by passing a time instace to the humanize.Time function. The output will be a nicely formatted date string in the form of 7 {unit} ago. See the example snippet below to learn about the usage.

package main

import (
    "fmt"
    "time"

    "github.com/dustin/go-humanize"
)

func main() {
    t := time.Now()
    fmt.Println(humanize.Time(t))
}

Sources


Subscribe to not miss out on new content.

Keep up with things amazing things happening in technology, business, software, and the web.

You will never receive spam and you can unsubscribe at any time.
Mykolas Krupauskas

Written by Mykolas Krupauskas. A passionate software developer from Lithuania, currently situated in the Netherlands. Helping people create value with technology.