如何在模板中把时间格式化成多久之前

goofcc · · 3389 次点击
David
Life is hard but please enjoy it!
写过一段程序,用 `SimpleDuration(time.Now().Sub(yourTime))`: type SimpleDuration time.Duration func (sd SimpleDuration) String() string { d := time.Duration(sd) if d.Hours() > 24 { return fmt.Sprintf("%.0f days", d.Hours()/24) } if d.Hours() >= 1 { return fmt.Sprintf("%.0f hours", d.Hours()) } if d.Minutes() >= 1 { return fmt.Sprintf("%.0f mins", d.Minutes()) } if d.Seconds() >= 1 { return fmt.Sprintf("%.0f sec", d.Seconds()) } if d.Nanoseconds() >= 1e6 { return fmt.Sprintf("%d ms", d.Nanoseconds()/1e6) } if d.Nanoseconds() >= 1e3 { return fmt.Sprintf("%d us", d.Nanoseconds()/1e3) } return fmt.Sprintf("%d ns", d.Nanoseconds()) }
#2
更多评论
David
Life is hard but please enjoy it!
https://github.com/daviddengcn/gcse/blob/master/server/smain.go#L80
#3