I am trying to convert a timestamp like this:
2015-06-27T09:34:22+00:00
to a time since format so it would say like 9 months ago 1 day 2 ho
Something like this would work, probably not the most efficient but it is as accurate as you gonna get:
func main() {
a := time.Date(2015, 10, 15, 0, 0, 0, 0, time.UTC)
b := time.Date(2016, 11, 15, 0, 0, 0, 0, time.UTC)
fmt.Println(monthYearDiff(a, b))
}
func monthYearDiff(a, b time.Time) (years, months int) {
m := a.Month()
for a.Before(b) {
a = a.Add(time.Hour * 24)
m2 := a.Month()
if m2 != m {
months++
}
m = m2
}
years = months / 12
months = months % 12
return
}
playground