timeago

package module
v3.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 8 Imported by: 1

README ¶

golang

Go Build Status Build Status Scrutinizer Code Quality GitHub

Timeago is a fast and lightweight date time package that converts given date into human readable "n time ago" format in different languages, such as 🇬🇧 🇷🇺 🇺🇦 🇳🇱 🇩🇪 🇨🇳 🇧🇾 🇪🇸 🇯🇵 🇫🇷. For more information you can read the documentation.

Follow the Official documentation for all the details

Quick Start

go get github.com/SerhiiCho/timeago/v3

Codebase naming

Here are some of the naming conventions used in the codebase and their meanings for better understanding:

  • time unit - a single time unit like second, seconds, minute, minutes, etc.
  • time number - a number of time units like 1, 2, 3, etc. in a string like 1 minute ago, 2 minutes ago, 3 minutes ago, etc.
  • suffix - the suffix ago in the final output like 1 minute ago, 2 minutes ago, 3 minutes ago, etc.
  • time since - the final output result like 2 minutes, 3 minutes ago, Just now, Online, 3 years ago etc.

Star History

Star History Chart

License

This project is open-sourced software licensed under the MIT license.

Contribute

With Container Engine

Build an Image

To build an image, navigate to the root of the project and run this command.

With Podman:

podman-compose build

With Docker:

docker compose build

Run the Container

To run a container, navigate to the root of the project and run this command to enter the Linux container with Go installed.

With Podman:

podman-compose run --rm app

With Docker:

docker compose run --rm app

Inside of this container you'll be able to run make commands like make test to run tests.

Documentation ¶

Index ¶

Constants ¶

View Source
const (
	LangEn string = "en" // English
	LangRu string = "ru" // Russian
	LangUk string = "uk" // Ukrainian
	LangNl string = "nl" // Dutch
	LangDe string = "de" // German
	LangZh string = "zh" // Chinese
	LangBe string = "be" // Belarusian
	LangEs string = "es" // Spanish
	LangJa string = "ja" // Japanese
	LangFr string = "fr" // French
)
View Source
const (
	// OptUpcoming option removes the suffix "ago" when the date is
	// in the future. This option is enabled by default, there
	// is no need to pass it. It's available to keep backward
	// compatibility with the previous versions.
	OptUpcoming opt = "upcoming"

	// Upcoming option displays "OptOnline" if date interval withing
	// the OnlineThreshold provided in config. By default, it's
	// 60 seconds. For example instead of "13 seconds ago" it will
	// print "Online".
	OptOnline opt = "online"

	// OptJustNow option displays "Just now" if date interval withing
	// the JustNowThreshold provided in config. By default, it's
	// 60 seconds. For example instead of "32 seconds ago" it will
	// print "Just now".
	OptJustNow opt = "justNow"

	// OptNoSuffix option removes suffix from datetime result and get
	// for example "5 minutes" instead of "5 minutes ago".
	OptNoSuffix opt = "noSuffix"
)

Variables ¶

This section is empty.

Functions ¶

func Configure ¶

func Configure(c Config)

Configure applies the given configuration to the timeago without overriding the previous configuration. It will only override the provided configuration. If you want to override the previous configurations, use Reconfigure function instead.

func Parse ¶

func Parse(date interface{}, opts ...opt) (string, error)

Parse coverts provided datetime into `x time ago` format. The first argument can different types of inputs: 1. Unix timestamp: int, int32, int64, unit, uint32, uint64, string 2. Type from Go time package: time.Time 3. Datetime string in format 'YYYY-MM-DD HH:MM:SS': string

func Reconfigure ¶

func Reconfigure(c Config)

Reconfigure reconfigures the timeago with the provided configuration. It will override the previous configuration with the new one.

Types ¶

type Config ¶

type Config struct {
	// Language is an ISO 639 language code like en, ru, de, nl, etc.
	// Default: LangEn ("en").
	Language string

	// Location is the timezone location needed for parsing
	// string date like "2019-01-01 00:00:00" to time.Time.
	// If Location is not set it will default to the server's.
	// Example: "America/New_York", "Europe/Moscow".
	// Default: "UTC".
	Location string

	// Translations is a slice of language sets that can be used to
	// overwrite the default translations. Read more about it in the docs
	// https://time-ago.github.io/configurations.html#overwrite-translations
	// Default: []LangSet{}
	Translations []LangSet

	// OnlineThreshold is the threshold in seconds to determine when
	// Timeago should show "Online" instead of "X seconds ago". If the
	// time difference is less than the threshold, it will show "Online".
	// Minimum value: "1"
	// Default: "60"
	OnlineThreshold int

	// JustNowThreshold is the threshold in seconds to determine when
	// Timeago should show "Just now" instead of "X seconds ago". If the
	// time difference is less than the threshold, it will show "Just now".
	// Minimum value: "1"
	// Default: "60"
	JustNowThreshold int
}

func NewConfig ¶

func NewConfig(lang, loc string, langSets []LangSet, online, justNow int) *Config

NewConfig creates a new Config instance with the given language, location and language sets to overwrite the default translations.

type LangForms ¶

type LangForms map[string]string

type LangSet ¶

type LangSet struct {
	Lang    string    `json:"lang"`
	Format  string    `json:"format"`
	Ago     string    `json:"ago"`
	Online  string    `json:"online"`
	JustNow string    `json:"justnow"`
	Second  LangForms `json:"second"`
	Minute  LangForms `json:"minute"`
	Hour    LangForms `json:"hour"`
	Day     LangForms `json:"day"`
	Week    LangForms `json:"week"`
	Month   LangForms `json:"month"`
	Year    LangForms `json:"year"`
}

type Rule ¶

type Rule struct {
	Zero  bool
	One   bool
	Two   bool
	Few   bool
	Many  bool
	Other bool
}

Directories ¶

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL