Timediff is a Go package for printing human readable, relative time differences 🕰️

Overview

Go Reference Go Report Card CI

timediff

timediff is a Go package for printing human readable, relative time differences. Output is based on ranges defined in the Day.js JavaScript library, and can be customized if needed.

go get github.com/mergestat/timediff
import (
    "github.com/mergestat/timediff"
)
str1 := timediff.TimeDiff(time.Now().Add(-10 * time.Second))
fmt.Println(str1) // a few seconds ago

str2 := timediff.TimeDiff(time.Now().Add(-3 * time.Minute))
fmt.Println(str2) // 3 minutes ago

str3 := timediff.TimeDiff(time.Now().Add(-23 * time.Hour))
fmt.Println(str3) // a day ago

str4 := timediff.TimeDiff(time.Now().Add(23 * time.Hour))
fmt.Println(str4) // in a day

str5 := timediff.TimeDiff(time.Now().Add(10 * time.Hour))
fmt.Println(str5) // in 10 hours

Here are examples of durations and their corresponding string outputs (taken from test output), using default options:

-864000h   100 years ago
-172800h   20 years ago
-17280h    2 years ago
-8640h     a year ago
-7200h1m   a year ago
-7200h     10 months ago
-6480h     9 months ago
-1920h     3 months ago
-1104h     2 months ago
-1080h     a month ago
-624h      a month ago
-600h      25 days ago
-240h      10 days ago
-36h       2 days ago
-24h       a day ago
-22h       a day ago
-21h30m    a day ago
-21h       21 hours ago
-20h       20 hours ago
-2h        2 hours ago
-90m       2 hours ago
-89m       an hour ago
-80m       an hour ago
-1h        an hour ago
-60m       an hour ago
-45m       an hour ago
-44m       44 minutes ago
-10m       10 minutes ago
-2m        2 minutes ago
-90s       2 minutes ago
-89s       a minute ago
-45s       a minute ago
-44s       a few seconds ago
-10s       a few seconds ago
10s        in a few seconds
44s        in a few seconds
45s        in a minute
89s        in a minute
90s        in 2 minutes
2m         in 2 minutes
10m        in 10 minutes
44m        in 44 minutes
45m        in an hour
60m        in an hour
1h         in an hour
80m        in an hour
89m        in an hour
90m        in 2 hours
2h         in 2 hours
20h        in 20 hours
21h        in 21 hours
21h30m     in a day
22h        in a day
24h        in a day
36h        in 2 days
240h       in 10 days
600h       in 25 days
624h       in a month
1080h      in a month
1104h      in 2 months
1920h      in 3 months
6480h      in 9 months
7200h      in 10 months
7200h1m    in a year
8640h      in a year
17280h     in 2 years
172800h    in 20 years
864000h    in 100 years
You might also like...
Clock is a small library for mocking time in Go.

clock Clock is a small library for mocking time in Go. It provides an interface around the standard library's time package so that the application can

A natural language date/time parser with pluggable rules

when when is a natural language date/time parser with pluggable rules and merge strategies Examples tonight at 11:10 pm at Friday afternoon the deadli

Copy of stdlib's time.Duration, but ParseDuration accepts other bigger units such as days, weeks, months and years

duration Copy of stdlib's time.Duration, but ParseDuration accepts other units as well: d: days (7 * 24 * time.Hour) w: weeks (7 * Day) mo: months (30

time format golang

a simple plugin to change date and time format

Show time by timezone

Show time by timezone

Go-timeparser - Flexible Time Parser for Golang

go-timeparser Flexible Time Parser for Golang Installation Download timeparser w

A Go package for working with dates

date Package date provides functionality for working with dates. This package introduces a light-weight Date type that is storage-efficient and conven

Go package for calculating the sunrise and sunset times for a given location

go-sunrise Go package for calculating the sunrise and sunset times for a given location based on this method. Usage To calculate sunrise and sunset ti

A Go package for working with dates

date Package date provides functionality for working with dates. This package introduces a light-weight Date type that is storage-efficient and coveni

Comments
  • Opposite operations

    Opposite operations

    Adding opposite operation (from string to time) will be useful. For example: "3 minutes ago" will be translating in "2022-02-04 15:23:00 UTC+3"

    opened by packman80 1
  • Adding ptBR locale and tests

    Adding ptBR locale and tests

    • Translated the english file to Portuguese (Brazilian Portuguese).
    • Added a separate test file to test only ptBR.
    • Using pt-BR as a default for Portuguese, since it's the only one present on the project at this time.
    opened by uilian 1
  • Extended functionality

    Extended functionality

    https://github.com/golang-module/carbon Is also locale aware and extends your currently functionality .

    It is of course golang.

    The human readable time diffs would be enhanced by using carbon because it unifies Multi locale DateTime calculations

    opened by gedw99 0
Releases(v0.0.3)
  • v0.0.3(Feb 2, 2022)

    What's Changed

    • Add support for defining locales by @riyaz-ali in https://github.com/mergestat/timediff/pull/4 🎉 🇮🇳
    • chore: also run CI on pull_request event by @patrickdevivo in https://github.com/mergestat/timediff/pull/5
    • Feat: add chinese local by @gozeon in https://github.com/mergestat/timediff/pull/6 🇨🇳
    • Add es-MX locale translations by @VictorAvelar in https://github.com/mergestat/timediff/pull/7 🇲🇽
    • feat: add ru-RU locale translations by @juev in https://github.com/mergestat/timediff/pull/8 🇷🇺
    • Add de-DE locale translations by @thorstenpfister in https://github.com/mergestat/timediff/pull/9 🇩🇪
    • Adding ptBR locale and tests by @uilian in https://github.com/mergestat/timediff/pull/10 🇵🇹
    • chore: move ru-RU tests to separate file by @patrickdevivo in https://github.com/mergestat/timediff/pull/11
    • Allow user supplied Formatters that override the ones provided by locales by @patrickdevivo in https://github.com/mergestat/timediff/pull/12

    New Contributors

    • @riyaz-ali made their first contribution in https://github.com/mergestat/timediff/pull/4
    • @gozeon made their first contribution in https://github.com/mergestat/timediff/pull/6
    • @VictorAvelar made their first contribution in https://github.com/mergestat/timediff/pull/7
    • @juev made their first contribution in https://github.com/mergestat/timediff/pull/8
    • @thorstenpfister made their first contribution in https://github.com/mergestat/timediff/pull/9
    • @uilian made their first contribution in https://github.com/mergestat/timediff/pull/10

    Full Changelog: https://github.com/mergestat/timediff/compare/v0.0.2...v0.0.3

    Source code(tar.gz)
    Source code(zip)
  • v0.0.2(Jan 25, 2022)

  • v0.0.1(Jan 25, 2022)

Owner
MergeStat
MergeStat enables SQL against source code and commit history
MergeStat
Golang package to manipulate time intervals.

timespan timespan is a Go library for interacting with intervals of time, defined as a start time and a duration. Documentation API Installation Insta

null 82 Sep 26, 2022
timeutil - useful extensions (Timedelta, Strftime, ...) to the golang's time package

timeutil - useful extensions to the golang's time package timeutil provides useful extensions (Timedelta, Strftime, ...) to the golang's time package.

Kyoung-chan Lee 190 Dec 22, 2022
Structural time package for jalali calendar

Jalali Structural time package for jalali calendar. This package support parse from string, json and time. Structures There are three data structures

Bardo Go Framework 1 Mar 21, 2022
Carbon for Golang, an extension for Time

Carbon A simple extension for Time based on PHP's Carbon library. Features: Time is embedded into Carbon (provides access to all of Time's functionali

Uniplaces LTD 742 Dec 20, 2022
:clock8: Better time duration formatting in Go!

durafmt durafmt is a tiny Go library that formats time.Duration strings (and types) into a human readable format. go get github.com/hako/durafmt Why

Wesley Hill 453 Dec 16, 2022
:clock1: Date and Time - Golang Formatting Library

Kair Date and Time - Golang Formatting Library Setup To get Kair > Go CLI go get github.com/GuilhermeCaruso/kair > Go DEP dep ensure -add github.com/G

Guilherme Caruso 24 Sep 26, 2022
Now is a time toolkit for golang

Now Now is a time toolkit for golang Install go get -u github.com/jinzhu/now Usage Calculating time based on current time import "github.com/jinzhu/n

Jinzhu 4k Dec 23, 2022
Go time library inspired by Moment.js

Goment Current Version: 1.4.0 Changelog Goment is a port of the popular Javascript datetime library Moment.js. It follows the Moment.js API closely, w

Nick Leeper 209 Dec 24, 2022
fasttime - fast time formatting for go

fasttime - fast time formatting for go

phuslu 67 Dec 13, 2022
🌐 A time zone helper

?? A time zone helper tz helps you schedule things across time zones. It is an interactive TUI program that displays time across a few time zones of y

Arnaud Berthomier 703 Dec 29, 2022