Outdated version
You are viewing the outdated version of Timeago library. Switch to the latest version to get all the new features and improvements
Guide
To read about the library, visit the What is Timeago? page.
Quick Start
Library versioning follows the Go Modules versioning. To get the latest version of the library, run the following command:
go get -u github.com/SerhiiCho/timeago/v2Usage
Pass the date to the timeago.Parse() function. It calculates the interval between the current datetime and the given datetime, returning a parsed string in the format x time ago. The library can work with both past and future dates. The usage is straightforward.
Allowed types
The timeago.Parse() function accepts different types of datetime:
intUnix timestamptime.TimeType from Go time packagestringDatetime string in formatYYYY-MM-DD HH:MM:SS
Any other type will trigger a panic.
timeago.Parse("2019-10-23 10:46:00") // string date
timeago.Parse(time.Now()) // time.Time
timeago.Parse(1642607826) // Unix timestampUsage with dates in the past
pastDate := time.Now().Add(-time.Hour)
res := timeago.Parse(pastDate)
fmt.Println(res) // 1 hour agoUsage with dates in the future
Future dates are also supported. The library will return the correct string without the word ago.
pastDate := time.Now().Add(time.Hour * 2)
res := timeago.Parse(pastDate)
fmt.Println(res) // 2 hours