Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created January 11, 2012 23:15
Show Gist options
  • Select an option

  • Save kylelemons/1597389 to your computer and use it in GitHub Desktop.

Select an option

Save kylelemons/1597389 to your computer and use it in GitHub Desktop.
Humanize a date! (go r60)
package main
import "fmt"
import "time"
func Humanize(t *time.Time) string {
const (
Minute = 60
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day
Month = 30 * Day
Year = 12 * Month
)
then, now := t.Seconds(), time.Seconds()
switch diff := now - then; {
case diff <= 0: return fmt.Sprintf("now")
case diff < 1*Minute: return fmt.Sprintf("%d seconds ago", diff)
case diff < 2*Minute: return fmt.Sprintf("1 minute ago")
case diff < 1*Hour: return fmt.Sprintf("%d minutes ago", diff/Minute)
case diff < 2*Hour: return fmt.Sprintf("1 hour ago")
case diff < 1*Day: return fmt.Sprintf("%d hours ago", diff/Hour)
case diff < 2*Day: return fmt.Sprintf("1 day ago")
case diff < 1*Week: return fmt.Sprintf("%d days ago", diff/Day)
case diff < 2*Week: return fmt.Sprintf("1 week ago")
case diff < 1*Month: return fmt.Sprintf("%d weeks ago", diff/Week)
case diff < 2*Month: return fmt.Sprintf("1 month ago")
case diff < 1*Year: return fmt.Sprintf("%d months ago", diff/Month)
case diff < 18*Month: return fmt.Sprintf("1 year ago")
}
return t.String()
}
// now
// 12 seconds ago
// 30 seconds ago
// 45 seconds ago
// 15 minutes ago
// 2 hours ago
// 21 hours ago
// 1 day ago
// 2 days ago
// 3 days ago
// 1 week ago
// 1 week ago
// 2 weeks ago
// 1 month ago
// 1 year ago
func ExampleHumanize() {
const (
Minute = 60
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day
Month = 30 * Day
Year = 12 * Month
)
now := time.Seconds()
fmt.Println(Humanize(time.SecondsToLocalTime(now)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-12)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-30)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-45)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-15*Minute)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-2*Hour)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-21*Hour)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-26*Hour)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-49*Hour)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-3*Day)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-7*Day)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-12*Day)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-15*Day)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-39*Day)))
fmt.Println(Humanize(time.SecondsToLocalTime(now-365*Day)))
}
@dustin

dustin commented Jan 13, 2012

Copy link
Copy Markdown

Hope you don't mind, but I put this in a humanize package since I needed one for file sizes. https://github.com/dustin/go-humanize

@kylelemons

kylelemons commented Jan 13, 2012 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment