andanhm / go-prettytime

Format GoLang dates in a “pretty” way. ex : just now, a minute ago, 2 hours ago , 3 minutes ago

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Pretty Time

Go Report Card Build Status GoDoc CodeCov

Format Go date time in a pretty way. ex : just now, a minute ago, 2 hours ago , 3 minutes ago

Inspired by the John Resig Pretty Date plug-in for JQuery

prettytime.Format("2008-01-28T20:24:17Z") // => "2 hours ago"
prettytime.Format("2008-01-27T22:24:17Z") // => "Yesterday"
prettytime.Format("2008-01-26T22:24:17Z") // => "2 days ago"
prettytime.Format("2008-01-14T22:24:17Z") // => "2 weeks ago"

Install

go get github.com/andanhm/go-prettytime

Example

package main

import (
	"log"
	"time"

	"github.com/andanhm/go-prettytime"
)

const (
	layout = "2006-01-02T15:04:05Z"
)

func main() {
	t, err := time.Parse(layout, "2008-01-28T20:24:17Z")
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%s \n", prettytime.Format(t))
}
// Output: 13 years ago

i18n support

go-prettytime uses go-i18n to provide i18n capabilities.

Translating a new language

Create an empty message file for the language that you want to add (e.g. translate.es.toml).

Run goi18n merge en.toml translate.es.toml to populate translate.es.toml with the messages to be translated.

After translate.es.toml has been translated, rename it to es.toml

Load es.toml into the time.go file via

bundle.MustLoadMessageFile("es.toml")

Contributions

Feel free to fork and add features, fix bugs and your pull request is more than welcome ❤

About

Format GoLang dates in a “pretty” way. ex : just now, a minute ago, 2 hours ago , 3 minutes ago

License:MIT License


Languages

Language:Go 100.0%